A
来源:互联网 发布:数据库系统实现英文版 编辑:程序博客网 时间:2024/06/05 08:36
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
#include <bits/stdc++.h>using namespace std;typedef long long ll;ll n,m,sum,mod;ll quick_pow(ll a,ll n){ ll result =1; while (n) { if (n&1) result=a*result%mod; a=a*a%mod; n>>=1; } return result ;}/****//推导过程1.在某一重循环中取得每个数字的概率为k/n2.一共循环n^k,3.n^k(k/n)化简:n^(k-1)*k*****/int main (){ int t,cnt ; //ll n,k,sum,mod; cin>>t; int x; cnt =1; ll ans ; while (t--) { sum=0; cin>>n>>m>>mod; for (int i=0;i<n;i++) { scanf("%d",&x); sum+=x; } if(sum>=mod) sum%=mod; ans=quick_pow(n,m-1)*m; ans=(ans*sum)%mod; printf("Case %d: %lld\n",cnt++,ans); } return 0;}