LightOj 1220 Fantasy of a Summation(快速幂)
来源:互联网 发布:手机能改淘宝密码吗 编辑:程序博客网 时间:2024/06/05 19:37
Fantasy of a Summation
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.
#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.'
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
2
3 1 35000
1 2 3
2 3 35000
1 2
Sample Output
Case 1: 6
Case 2: 36
解题思路:
代码中有k层循环,每层循环n次,即进行了n^k次加法。每一次加法从n个数中可重复的选取k个数,每个数被选择的概率是k/n,所
有每个数对答案的贡献就是k*n^(k-1).
AC代码:
#include <bits/stdc++.h>using namespace std;typedef long long ll;ll n,k,MOD;ll quick_pow(ll base,ll n){ ll ans = 1; while(n){ if(n&1) ans = ans*base%MOD; base = base*base%MOD; n >>= 1; } return ans;}int main(){ int T,t = 0; scanf("%d",&T); while(T--){ scanf("%lld%lld%lld",&n,&k,&MOD); ll x,ans = 0; for(int i = 0; i < n; ++i){ scanf("%lld",&x); ans += x; } ans = ans%MOD; printf("Case %d: %lld\n",++t,ans*quick_pow(n,k-1)*k%MOD); } return 0;}
- LightOj 1220 Fantasy of a Summation(快速幂)
- LightOJ 1213 - Fantasy of a Summation (排列组合+快速幂)
- LightOJ 1213 Fantasy of a Summation(快速幂)
- lightOJ 1213 Fantasy of a Summation
- LightOJ 1213 Fantasy of a Summation
- LightOJ 1213 Fantasy of a Summation
- Fantasy of a Summation
- Fantasy of a Summation
- Light OJ 1213 Fantasy of a Summation (快速幂+规律)
- lightoj 1213 - Fantasy of a Summation 【数学计数】
- light oj 1220 Fantasy of a Summation
- lightoj1213 - Fantasy of a Summation
- LightOJ1213 Fantasy of a Summation
- light oj 1213 - Fantasy of a Summation (数学计数)
- Fantasy of a Summation(找规律,水)
- 【Light】[1213]Fantasy of a Summation
- ACM_1007_Numerical Summation of a Series
- Numerical Summation of a Series
- Error:Cause: org/gradle/api/publication/maven/internal/DefaultMavenFactory AndroidStudio
- POJ 2528(并查集)
- NYOJ_1241_Distribution
- leetcode-09-Palindrome Number
- Java简介
- LightOj 1220 Fantasy of a Summation(快速幂)
- 前端开发学习笔记02----HTML基础
- 人见人爱A^B
- objective-c之block 总结
- 在Eclipse中打开工程中的drawable里面的PNG格式的图片报错,找不到应用程序
- java实现子字符串的BM算法
- kafka源码解析之十一KafkaApis
- Jmeter和Tsung之间的对比
- Mod of power 2 on bitwise operators?