快速幂——L - Fantasy of a Summation LightOJ - 1213
作者:互联网
L - Fantasy of a Summation
快速幂,每个元素的次数k*n^(k-1),模完了加就行。要记得在每个位置都模一下
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <map> 5 #include <set> 6 #include <algorithm> 7 #include <fstream> 8 #include <cstdio> 9 #include <cmath> 10 #include <stack> 11 #include <queue> 12 using namespace std; 13 const double Pi=3.14159265358979323846; 14 typedef long long ll; 15 const int MAXN=5000+5; 16 const int dx[5]={0,0,0,1,-1}; 17 const int dy[5]={1,-1,0,0,0}; 18 const int INF = 0x3f3f3f3f; 19 const int NINF = 0xc0c0c0c0; 20 ll mod_pow(ll n,ll x,ll mod) 21 { 22 ll ans=1; 23 while(n>0) 24 { 25 if(n&1) ans=ans*x%mod; 26 x=x*x%mod; 27 n>>=1; 28 } 29 return ans; 30 } 31 32 ll a[MAXN]; 33 int main() 34 { 35 int t; 36 cin>>t;int cnt=0; 37 while(t--) 38 { 39 ll k,n,mod;cin>>n>>k>>mod; 40 ll sum=0;ll bur=(k%mod*mod_pow(k-1,n,mod))%mod; 41 for(int i=1;i<=n;i++) 42 { 43 cin>>a[i]; 44 sum=(sum+(bur*(a[i]%mod)%mod))%mod; 45 } 46 printf("Case %d: %lld\n",++cnt,sum); 47 } 48 return 0; 49 }
标签:const,int,ll,LightOJ,Fantasy,Summation,include,sum,mod 来源: https://www.cnblogs.com/Msmw/p/10991317.html