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.
#include <stdio.h>
int cases, caseno;
int n, K, MOD;
int A[1001];
int main() {
scanf("%d", &cases);
while( cases-- ) {
scanf("%d %d %d", &n, &K, &MOD);
int i, i1, i2, i3, ... , iK;
for( i = 0; i < n; i++ ) scanf("%d", &A[i]);
int res = 0;
for( i1 = 0; i1 < n; i1++ ) {
for( i2 = 0; i2 < n; i2++ ) {
for( i3 = 0; i3 < n; i3++ ) {
...
for( iK = 0; iK < n; iK++ ) {
res = ( res + A[i1] + A[i2] + ... + A[iK] ) % MOD;
}
...
}
}
}
printf("Case %d: %d\n", ++caseno, res);
}
return 0;
}
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.'
InputInput 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.
For each case, print the case number and result of the code.
2
3 1 35000
1 2 3
2 3 35000
1 2
Case 1: 6
Case 2: 36
题意:给出一段代码看懂简化下,这题就是说有k层循环每层遍历n个数字,最后把k层遍历到的数字全部加和取模。
思路:
稍微推下公式就有res=n^k*k/n*sum%mod;就是一共有n^k种组合,每种组合k个数字,平均分给n个数字,之后快速幂解决。
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int MAXN = 1e7+10;
int a[1003];
LL qp(LL n,LL m,LL mod)
{
LL ans=1;
while(m)
{
if(m&1)
{
ans=(ans*n)%mod;
}
n=(n*n)%mod;
m=m>>1;
}
return ans;
}
int main()
{
int t;
cin>>t;
int g=0;
while(t--)
{
++g;
LL n,k,mod;
cin>>n>>k>>mod;
LL sum=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}
LL sum1=qp(n,k-1,mod),sum2;
// cout<<"**"<<sum<<endl;
// cout<<sum1<<endl;
sum2=sum1%mod*k%mod*sum%mod;
printf("Case %d: %lld\n",g,sum2);
}
return 0;
}