LightOJ - 1213 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 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

排列组合一下会发现,每一个元素加和的次数为 n的k-1次方乘以k、

#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<map>
#include<iostream>
#include<string.h>
#include<algorithm>
#define maxn 1000000
#define maxm 10000005
#define MAXN 100005
#define MAXM 10005
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
ll pow_mod(ll a,ll i,ll n){
    if(i==0)return 1%n;
    ll temp=pow_mod(a,i>>1,n);
    temp=temp*temp%n;
    if(i&1)temp=(ll)temp*a%n;
    return temp;
}
int main(){
    int t,test=0;
    scanf("%d",&t);
    while(t--){
     ll n,k,mod;ll sum=0;
     scanf("%lld%lld%lld",&n,&k,&mod);
     for(int i=0;i<n;i++){
        ll x;scanf("%d",&x);sum+=x;
     }
     printf("Case %d: %lld\n",++test,(((pow_mod(n,k-1,mod)*sum)%mod)*k)%mod);
    }
}

posted @ 2018-05-15 20:19  _大美  阅读(244)  评论(0编辑  收藏  举报