快速幂——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

时间: 2024-10-21 09:57:31

快速幂——L - Fantasy of a Summation LightOJ - 1213的相关文章

Fantasy of a Summation LightOJ - 1213 (快速幂)

题意: 首先 只看第一层循环的A[0],是不是用了nk-1次  A[1]也是用了nk-1次······ 所以 第一层的sum(A[i]的和) 一共用了nk-1 所以第一层为sum * nk-1 因为又k层循环 所以全部为sum * nk-1 * k 最后不要忘了 % MOD 代码如下: #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #define mem

LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)

http://lightoj.com/volume_showproblem.php?problem=1213 Fantasy of a Summation Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1213 Description If you think codes, eat codes then sometimes you

LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)

题目链接:LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 得到递推式之后就是矩阵快速幂了 注意:模2^64,定义成unsigned long long 类型,因为无符号类型超过最大范围的数与该数%最大范围 的效果是一样的. AC代码: #include<stdio.h> #include<string.h> #define LL unsigned long long struct Ma

LightOJ 1132 - Summing up Powers 矩阵快速幂+排列组合

链接:http://lightoj.com/volume_showproblem.php?problem=1132 1132 - Summing up Powers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given N and K, you have to find (1K + 2K + 3K + ... + NK) % 232 Input Input starts with an i

HDU - 6027 Easy Summation(快速幂)解题

Easy Summation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1107    Accepted Submission(s): 443 Problem Description You are encountered with a traditional problem concerning the sums of po

LightOJ 1070 - Algebraic Problem 矩阵快速幂

题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessar

hdu6027Easy Summation(快速幂取模)

Easy Summation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 4112    Accepted Submission(s): 1676 Problem Description You are encountered with a traditional problem concerning the sums of po

快速幂 E - Leading and Trailing LightOJ - 1282

E - Leading and Trailing LightOJ - 1282 快速幂主要是把n拆成2进制位,如果这一位有那么就乘,没有就不乘,而计数器也就是x是不断推进的,从x->x^2->x^4直到n的最高位精髓在于取模,后一步的要求结果只与前一步的模后数据有关 . 对于后三个数用了log10.log函数对求n^k这种问题还是很有用的.没想出来. 1 #include <iostream> 2 #include <cstring> 3 #include <st

ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 3 c