牛客每日练习----杨老师的游戏,Max Convolution,Walking in the Forest

本文探讨了两道算法题目,一是利用1-9数字构建特定数值的数学表达式,二是优化森林穿越路径的步长问题。涉及递归、组合数学及二分查找等算法技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我喜欢给自己压力,必须得定一个很高的目标,逼自己朝着这个目标前进,不管会不会实现,都是一个动力。                                      ----喻言

链接:https://ac.nowcoder.com/acm/problem/15823
来源:牛客网
 

题目描述

杨老师给同学们玩个游戏,要求使用乘法和减法来表示一个数,他给大家9张卡片,然后报出一个数字,要求大家用表达式的形式来表示出这个数
100 可以表示为这样的形式:100 = 129*67-8543 , 还可以表示为:100 = 13*489-6257
注意特征:表达式中,数字1~9分别出现且只出现一次(不包含0)。
类似这样的表达式,100 有 20 种表示法。
题目要求:
从标准输入读入一个正整数N(N<1000 * 1000)
程序输出该数字用数码1~9不重复不遗漏地组成的全部种数。
注意:不要求输出每个表示,只统计有多少表示法!

输入描述:

一个正整数N

输出描述:

输出有多少种表示法

示例1

输入

复制

100

输出

复制

20

备注:

注意只有一个乘法和一个减法,*号保证在-的前面
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
typedef long long ll;
using namespace std;
int const N=1e5+10;
int const mod=1e9+7;
const int maxn=1e5+10;
int n,a[10],vis[10],ct;
void hhs(int sd)
{
    if(sd==9){
 
        int x=0,y=0,z=0;
        for(int i=1;i<=9;i++){
            x=x*10+a[i];
            y=0;
            for(int j=i+1;j<=9;j++){
                y=y*10+a[j];
                z=0;
                for(int k=j+1;k<=9;k++)
                    z=z*10+a[k];
                if(x*y-z==n) 
					ct++;
            }
        }
    }
    for (int i=1;i<=9;i++){
        if(vis[i]==0){
            a[sd+1]=i;
            vis[i]=1;
            hhs(sd+1);
            vis[i]=0;
        }
    }
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=9;i++){
        a[1]=i;
        vis[i]=1;
        hhs(1);
        vis[i]=0;
    }
    printf("%d\n",ct);
    return 0;
}

链接:https://ac.nowcoder.com/acm/problem/15839
来源:牛客网
 

题目描述

Alice has an array a0a1...an−1a_0a_1...a_{n-1}a0​a1​...an−1​, she want to calculate the other array b0b1...bn−1b_0b_1...b_{n-1}b0​b1​...bn−1​ where

Can you help her? For simplicity, please output ∑i=0n−1(i+1)⋅bi mod 1000000007\sum_{i=0}^{n-1} (i+1)\cdot b_i \bmod 1000000007∑i=0n−1​(i+1)⋅bi​mod1000000007

输入描述:

In the first line there are two integer n, m,(1 <= n <= 100000, 1 <= m <= 1000000000).
In the second line there are n integersa0,a1,...,an−1(1<=ai<=1000000000)a_0,a_1,...,a_{n-1} (1 <= a_i <= 1000000000)a0​,a1​,...,an−1​(1<=ai​<=1000000000) .

输出描述:

Output a single integer, which is∑i=0n−1(i+1)⋅bi mod 1000000007\sum_{i=0}^{n-1} (i+1)\cdot b_i \bmod 1000000007∑i=0n−1​(i+1)⋅bi​mod1000000007

示例1

输入

复制

5 2
1 10 6 10 8

输出

复制

4985

说明

b0=a0⋅a0=1b_0 = a_0\cdot a_0 = 1b0​=a0​⋅a0​=1
b1=a0⋅a1+a1⋅a0+a1⋅a1=120b_1 = a_0\cdot a_1 + a_1\cdot a_0 + a_1\cdot a_1= 120b1​=a0​⋅a1​+a1​⋅a0​+a1​⋅a1​=120
b2=a0⋅a2+a1⋅a2+a2⋅a0+a2⋅a1+a2⋅a2=168b_2 = a_0\cdot a_2 + a_1\cdot a_2 + a_2\cdot a_0 + a_2\cdot a_1 + a_2\cdot a_2 = 168b2​=a0​⋅a2​+a1​⋅a2​+a2​⋅a0​+a2​⋅a1​+a2​⋅a2​=168
b3=a0⋅a3+a1⋅a3+a2⋅a3+a3⋅a0+a3⋅a1+a3⋅a2+a3⋅a3=440b_3 = a_0\cdot a_3 + a_1\cdot a_3 + a_2\cdot a_3 + a_3\cdot a_0 + a_3\cdot a_1 + a_3\cdot a_2 + a_3\cdot a_3 = 440b3​=a0​⋅a3​+a1​⋅a3​+a2​⋅a3​+a3​⋅a0​+a3​⋅a1​+a3​⋅a2​+a3​⋅a3​=440
b4=a0⋅a4+a1⋅a4+a2⋅a4+a3⋅a4+a4⋅a0+a4⋅a1+a4⋅a2+a4⋅a3+a4⋅a4=496b_4 = a_0\cdot a_4 + a_1\cdot a_4 + a_2\cdot a_4 + a_3\cdot a_4 + a_4\cdot a_0 + a_4\cdot a_1 + a_4\cdot a_2 + a_4\cdot a_3 + a_4\cdot a_4 = 496b4​=a0​⋅a4​+a1​⋅a4​+a2​⋅a4​+a3​⋅a4​+a4​⋅a0​+a4​⋅a1​+a4​⋅a2​+a4​⋅a3​+a4​⋅a4​=496
ans=1⋅b0+2⋅b1+3⋅b2+4⋅b3+5⋅b4=4985ans = 1\cdot b_0 + 2\cdot b_1 + 3\cdot b_2 + 4\cdot b_3 + 5\cdot b_4 = 4985ans=1⋅b0​+2⋅b1​+3⋅b2​+4⋅b3​+5⋅b4​=4985

示例2

输入

复制

10 3
83254494 79256570 664815211 929105503 348307749 129917295 141270181 116122929 432020021 461745049

输出

复制

964655557
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
typedef long long ll;
using namespace std;
int const N=1e5+10;
int const mod=1e9+7;
const int maxn=1e5+10;
ll A[N],B[N],C[N];
ll ksm(ll a,ll b,ll m){
    ll ans=1;
    while(b){
        if(b&1
			)ans=(ans*a)%m;
        b>>=1;
        a=a*a%m;
    }
    return ans;
}
int main(){
    ll n,m;
    cin>>n>>m;
    for(int i=1;i<=n;++i)
        scanf("%lld",&A[i]);
    for(int i=1;i<=n;++i){
        B[i]=B[i-1]+A[i];
        B[i]%=mod;
    }
    for(int i=1;i<=n;++i)
        C[i]=(ksm(B[i],m,mod)-ksm(B[i-1],m,mod)+mod)%mod;
    ll jg=0;
    for(int i=1;i<=n;++i)
        jg=(jg+i*C[i]%mod)%mod;
    cout<<jg%mod<<endl;
    return 0;
}

链接:https://ac.nowcoder.com/acm/problem/15817
来源:牛客网
 

题目描述

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.

Now you're going to walk through a large forest. There is a path consisting of N stones winding its way to the other side of the forest. Between every two stones there is a distance. Let di indicates the distance between the stone i and i+1.Initially you stand at the first stone, and your target is the N-th stone. You must stand in a stone all the time, and you can stride over arbitrary number of stones in one step. If you stepped from the stone i to the stone j, you stride a span of (di+di+1+...+dj-1). But there is a limitation. You're so tired that you want to walk through the forest in no more than K steps. And to walk more comfortably, you have to minimize the distance of largest step.

输入描述:

The first line contains two integer N and K(1≤K≤N≤105)( 1\leq K\leq N \leq 10^{5})(1≤K≤N≤105) as described above.

Then the next line N-1 positive integer followed, indicating the distance between two adjacent stone(1≤ai≤105)(1 \leq a_{i}\leq 10^{5} )(1≤ai​≤105).

输出描述:

An integer, the minimum distance of the largest step.

示例1

输入

复制

6 3
1 3 2 2 5

输出

复制

5
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
typedef long long ll;
using namespace std;
int const N=10010;
int const mod=998244353;
const int maxn=1e5+10;
ll a[100000+10],n,k;
bool jc(ll x)
{
    int ct=0;
    ll tmp=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]>x)
			return false;
        if(a[i]+tmp<x)
            tmp+=a[i];
        else{
            ct++;
            if(tmp+a[i]==x)
				tmp=0;
            else 
				tmp=a[i];
        }
    }
    if(tmp)
		ct++;
    return ct<=k;
}
int main()
{
    cin>>n>>k;
    n--;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    ll r=10000000010,l=0,mid,jg=0;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(jc(mid))
        {
            jg=mid;
            r=mid-1;
        }
        else
            l=mid+1;
    }
    cout<<jg<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值