快速幂——L - Fantasy of a Summation LightOJ - 1213

L - Fantasy of a Summation

 LightOJ - 1213 

快速幂,每个元素的次数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 }

 

转载于:https://www.cnblogs.com/Msmw/p/10991317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值