1213 - Fantasy of a Summation
#include <stdio.h>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.
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.'
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
For each case, print the case number and result of the code.
Sample Input |
Output for Sample Input |
23 1 350001 2 32 3 35000
1 2 |
Case 1: 6Case 2: 36 |
题目类型:简单数学
算法分析:可以直接计算出每一位中可能出现的数的和,然后再乘以位数k就可以了,公式为sum * (n ^ (k - 1)) * k % mod,sum表示一位中所有数的总和,n ^ (k - 1)表示在一位中出现一种数字的次数,由于数据比较大,所以需要使用整数快速幂取模来计算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 50000 + 66; long long mult_mod (long long a,long long b,long long mod) { a %= mod; b %= mod; long long ans = 0; long long temp = a; while (b) { if (b & 1) { ans += temp; if (ans > mod) ans -= mod;//直接取模慢很多 } temp <<= 1; if (temp > mod) temp -= mod; b >>= 1; } return ans; } long long pow_mod (long long a,long long n,long long mod) { long long ans = 1; long long temp = a % mod; while (n) { if (n & 1) ans = mult_mod (ans, temp, mod); temp = mult_mod (temp, temp, mod); n >>= 1; } return ans; } int main() { // freopen ("aaa.txt", "r", stdin); int t, flag = 1; scanf ("%d", &t); while (t--) { printf ("Case %d: ", flag++); long long temp, sum = 0, n, k, mod; scanf ("%lld%lld%lld", &n, &k, &mod); for (long long i = 0; i < n; i++) { scanf ("%lld", &temp); sum += temp; } long long ans = pow_mod (n, k - 1, mod); ans = (ans % mod) * (sum % mod) % mod; ans = (ans % mod) * (k % mod) % mod; printf ("%lld\n", ans); } return 0; } |
- « 上一篇:lightoj1212
- lightoj1215:下一篇 »