NC15839. Max Convolution
描述
输入描述
In the first line there are two integer n, m,(1 <= n <= 100000, 1 <= m <= 1000000000).
In the second line there are n integers .
输出描述
Output a single integer, which is
示例1
输入:
5 2 1 10 6 10 8
输出:
4985
说明:
示例2
输入:
10 3 83254494 79256570 664815211 929105503 348307749 129917295 141270181 116122929 432020021 461745049
输出:
964655557
C++14(g++5.4) 解法, 执行用时: 65ms, 内存消耗: 1512K, 提交时间: 2019-02-14 22:09:20
#include<stdio.h> #define fo(i,a,b) for(int i=a;i<=b;i++) const int p=1e9+7; int n,m,a[110000]; long long b[110000],ans; inline long long qpow(long long a,int n){ long long ans=1; while (n){ if (n&1) ans=ans*a%p; a=a*a%p; n>>=1; } return ans; } int main(){ scanf("%d%d",&n,&m); fo(i,1,n){ scanf("%d",&a[i]); a[i]+=a[i-1]; if (a[i]>=p) a[i]-=p; b[i]=qpow(a[i],m)-qpow(a[i-1],m); if (b[i]<0) b[i]+=p; ans+=i*b[i]%p; } ans%=p; printf("%lld\n",ans); return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 60ms, 内存消耗: 2916K, 提交时间: 2020-09-25 23:16:46
#include<iostream> using namespace std; typedef long long ll; const ll mod=1e9+7; ll va[100005],su[100005]={0}; ll poww(ll x,ll n){ ll ans=1; while(n){ if(n&1) ans=ans*x%mod; x=x*x%mod;n>>=1; }return ans; } int main(){ ll n,m,ans=0;cin>>n>>m; for(int i=1;i<=n;i++) cin>>va[i],su[i]=(va[i]+su[i-1])%mod; for(int i=1;i<=n;i++) ans+=((poww(su[i],m)-poww(su[i-1],m)+mod)%mod*i)%mod,ans=ans%mod; cout<<ans; }