poj1213Fantasy of a Summation(找规律,优化代码)
来源:互联网 发布:dos复制文件到linux 编辑:程序博客网 时间:2024/06/01 10:52
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
按代码写肯定会超时,那么该如何优化代码呢?
观察代码:
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重循环,简单模拟,设有3重循环,2个数,
10002001301040115100610171108111
共进行2^3次操做因为每个数的累加次数一样多的
它每个数的累加次数为(n^k)*k/n=(n^(k-1))*k,
则和就等于(sum(a[i])*(n^(k-1))*k)%mod
#include<stdio.h>#include<algorithm>#include<iostream>#define LL long longusing namespace std; LL gcd(LL a,LL b){return b?gcd(b,a%b):a;} LL lcm(LL a,LL b){return a/gcd(a,b)*b;} LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} int main() { int t; int cas=0; scanf("%d",&t); while(t--) { int n,k,mod; scanf("%d%d%d",&n,&k,&mod); LL ans=0; for(int i=0;i<n;i++) { int a; scanf("%d",&a); ans=(ans+a)%mod; } LL num=powmod(n,k-1,mod); printf("Case %d: ",++cas); printf("%d\n",(ans*num*k)%mod); } return 0; }
- poj1213Fantasy of a Summation(找规律,优化代码)
- Fantasy of a Summation(找规律,水)
- Light OJ 1213 Fantasy of a Summation (快速幂+规律)
- Fantasy of a Summation
- Fantasy of a Summation
- ACM_1007_Numerical Summation of a Series
- Numerical Summation of a Series
- zoj1007Numerical Summation of a Series
- lightoj1213 - Fantasy of a Summation
- Numerical Summation of a Series
- LightOJ1213 Fantasy of a Summation
- LightOj 1220 Fantasy of a Summation(快速幂)
- light oj 1213 - Fantasy of a Summation (数学计数)
- LightOJ 1352 Strange Summation(找规律or数位DP)
- HDOJ1564Play a game(找规律博弈)
- A - 汉诺塔III (找规律)
- LightOJ 1352 Strange Summation(规律)
- zoj 1007 Numerical Summation of a Series
- UVA - 297 Quadtrees
- 面向对象
- UVA301Transportation
- JS+CSS 实现 悬浮滚动广告
- Git知识整理
- poj1213Fantasy of a Summation(找规律,优化代码)
- FFT模板
- 原码补码反码之间的关系
- python 设置线程名
- mongodb c++ API
- 5.4 编写一个程序,求以下矩形两条对角线上的元素之和:
- linux后台运行任务——screen
- 树莓派3B安装QT5
- coderforces 670D2 Magic Powder - 2