http://lightoj.com/volume_showproblem.php?problem=1213
Fantasy of a summation
Time Limit:2000MS
Memory Limit:32768KB
64bit IO Format:%LLD &%llusubmit Status Practice Lightoj 1213
Description
If you are think codes, eat codes then sometimes your may get stressed. In your dreams the see huge codes, as I has seen once. Here's 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 on: ' You're given three integers n, K, MOD and n integers: C5>A0, A1, A2 ... An-1, you had to write K nested loops and calculate the summation of all Ai where i am th e 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 is fit into a A-bit signed integer.
Output
For each case, print the case number and result of the code.
Sample Input
2
3 1 35000
1 2 3
2 3 35000
1 2
Sample Output
Case 1:6
Case 2:36
According to the code attached to the topic is not difficult to see test instructions, no longer say formula (sum*n^ (k-1) * k)% mod finally introduced the formula, the results just take the remaining error, depressed half a day!!!! n^ (k-1) to use the fast power to solve in order to prevent overflow to the fullest to take the rest ...
#include <stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespacestd;Const intN =1010;Const intINF =0x3f3f3f3f; typedefLong Longll;intmod;ll Pow (intAintBintc) {ll ans=1; A%=C; while(b) {if(b%2==1) ans= (ans * a)%C; A= (A * a)%C; b/=2; } returnans;}intMain () {intT, A[n], p =0;; intN, K; ll sum; scanf ("%d", &t); while(t--) {p++; Sum=0; scanf ("%d%d%d", &n, &k, &MoD); for(inti =0; I < n; i++) {scanf ("%d", &A[i]); Sum+=A[i]; } ll S; S= Pow (n, K-1, MoD); S*=K; Sum%=MoD; Sum*=s; Sum%=MoD; printf ("Case %d:%lld\n", p, sum); } return 0;}/*4 304 9 522 2 222 2147483647 33332147483647 2147483647*/
Lightoj 1213 Fantasy of a summation (rule + fast power)