E - Fantasy of a Summation LightOJ1213
Posted dy blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E - Fantasy of a Summation LightOJ1213相关的知识,希望对你有一定的参考价值。
E - Fantasy of a Summation
Problem Description
If you think codes, eat codes then sometimes you may get stressed. In your dreams you may see huge codes, as I have seen once. Here is the code I saw in my dream.
Actually the code was about: ‘You are given three integers n, K, MOD and n integers: A0, A1, A2 ... An-1, you have to write K nested loops and calculate the summation of all Ai where i is the value of any nested loop variable.‘
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with three integers: n (1 ≤ n ≤ 1000), K (1 ≤ K < 231), MOD (1 ≤ MOD ≤ 35000). The next line contains n non-negative integers denoting A0, A1, A2 ... An-1. Each of these integers will be fit into a 32 bit signed integer.
Output
Sample Input
2 3 1 35000 1 2 3 2 3 35000 1 2
Sample Output
Case 1: 6 Case 2: 36
1 #include <stdio.h> 2 #include <queue> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <ctime> 10 #include <cmath> 11 #include <cctype> 12 using namespace std; 13 typedef long long LL; 14 const int N=1e5+10; 15 const int INF=0x3f3f3f3f; 16 int cas=1,T; 17 int n,k,mod; 18 int quick_mod(int t,int n) 19 { 20 int ans=1; 21 while(n) 22 { 23 if(n&1) ans=ans*t%mod; 24 t=t*t%mod; 25 n>>=1; 26 } 27 return ans; 28 } 29 int main() 30 { 31 //freopen("1.in","w",stdout); 32 // freopen("1.in","r",stdin); 33 // freopen("1.out","w",stdout); 34 scanf("%d",&T); 35 while(T--) 36 { 37 scanf("%d%d%d",&n,&k,&mod); 38 int ans=0; 39 for(int i=0,x;i<n;i++) 40 { 41 scanf("%d",&x); 42 ans+=x%mod; 43 } 44 ans%=mod; 45 ans=k%mod * quick_mod(n,k-1) % mod *ans %mod; 46 printf("Case %d: %d\n",cas++,ans); 47 } 48 //printf("time=%.3lf\n",(double)clock()/CLOCKS_PER_SEC); 49 return 0; 50 }
题解:
仔细研究一下代码会发现只是一条公式:k*n^(k-1)*(a1+a2+...+an)
然后快速幂即可,注意不要被循环绕晕
以上是关于E - Fantasy of a Summation LightOJ1213的主要内容,如果未能解决你的问题,请参考以下文章
快速幂——L - Fantasy of a Summation LightOJ - 1213
FANTASY:In which way do you think the world will end?
[CSP-S模拟测试]:Endless Fantasy(DFS)