Fantasy of a Summation
来源:互联网 发布:java thread import 编辑:程序博客网 时间:2024/05/16 11:31
Fantasy of a Summation
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 writeK nested loops and calculate the summation of all Ai wherei is the value of any nested loop variable.'
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 containsn 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
这种题可能算一种类型吧,即推公式,算出每个值对于 答案 的贡献 ,或者说是每个值被选中的概率 。
题目很长,就是让你求给定的那段代码所能求出的值,显然你不能用他写的那段代码。
从给定的代码上可以看出: 每次进行一个 n 的循环,即进行了 n^k 次的加法,而每次的求和都是重复的选取n个数,所以每个数的贡献就是k*n^(k-1);
所以实现就是用个快速幂遍历一遍。
代码:
#include<iostream>#include<cstdio>#include<cmath>using namespace std;typedef long long ll;ll n,k,mod;ll Pow(ll a,ll b){ ll ans=1; a%=mod; while(b) { if(b&1) ans=ans*a%mod; b>>=1; a=a*a%mod; } return ans;}int main(){ int T; int kase=1; scanf("%d",&T); while(T--) { scanf("%lld%lld%lld",&n,&k,&mod); ll xx=0,x; for(ll i=0; i<n; i++) { scanf("%lld",&x); xx+=x; } xx%=mod; ll sum=(k*Pow(n,(k-1))*xx)%mod; printf("Case %d: ",kase++); printf("%lld\n",sum); } return 0;}
- Fantasy of a Summation
- Fantasy of a Summation
- lightoj1213 - Fantasy of a Summation
- LightOJ1213 Fantasy of a Summation
- lightOJ 1213 Fantasy of a Summation
- LightOJ 1213 Fantasy of a Summation
- 【Light】[1213]Fantasy of a Summation
- LightOJ 1213 Fantasy of a Summation
- light oj 1220 Fantasy of a Summation
- LightOJ 1213 - Fantasy of a Summation (排列组合+快速幂)
- lightoj 1213 - Fantasy of a Summation 【数学计数】
- LightOj 1220 Fantasy of a Summation(快速幂)
- LightOJ 1213 Fantasy of a Summation(快速幂)
- light oj 1213 - Fantasy of a Summation (数学计数)
- Fantasy of a Summation(找规律,水)
- Light OJ 1213 Fantasy of a Summation (快速幂+规律)
- ACM_1007_Numerical Summation of a Series
- Numerical Summation of a Series
- madplay移植
- Python 文件读写基础操作实例
- 面试题及答案
- dos窗口下运行java可以,运行javac却报错
- 突然想到一道简单题,但也有深意!!
- Fantasy of a Summation
- Json序列化之.NET开源类库Newtonsoft.Json的研究
- 数据结构排序法之希尔排序法(Shell Sort)
- 【HDU 1875】畅通工程再续(最小生成树-水题)
- 第一天
- Android 官方下拉刷新SwipeRefreshLayout使用
- base64学习笔记
- 排序算法(三)交换类排序:冒泡排序和快速排序
- DOM包裹wrap()方法