题目链接:
LightOJ 1213 Fantasy of a Summation
题意:
给定n,k,m求解:
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;}...}}
}
分析:
其实就是k层循环,每层遍历n个数字,把遍历到的数字相加和取模。模拟一下可以得到:ans = sum * n^(k -1) * k
。sum是n个数字之和。然后就是快速幂计算下就好了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#include <set>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 1010;int T, n, total, cases = 0, K;
ll sum, mod, data[MAX_N];ll quick_pow(ll a, ll b)
{ll res = 1, tmp = a;while(b){if(b & 1) res = res * tmp % mod;tmp = tmp * tmp % mod;b >>= 1;}return res;
}int main()
{scanf("%d", &T);while(T--){sum = 0;scanf("%d%d%lld", &n, &K, &mod);for(int i = 0; i < n; i++){scanf("%lld", &data[i]);sum += data[i];sum %= mod;}ll ans = quick_pow(n, K - 1) * K % mod * sum % mod;printf("Case %d: %lld\n", ++cases, ans);}return 0;
}