码迷,mamicode.com
首页 > 其他好文 > 详细

light_oj 1213

时间:2015-06-09 23:30:19      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

light_oj 1213  

L - Fantasy of a Summation
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

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 nKMOD 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

题意:优化给定代码
思路:首先明确操作对每个a[i]是等效的,即每个a[i]被计算的次数是一样的,因此可以从概率方面考虑,有k层循环,每层循环有n次操作,每次操作等概率地取出a[i]加到res中,概率显然是每次循环从n个取出k个中每一次的概率,即k/n,而总共有n^k次,因此每个a[i]被加了k/n*n^k=k*n^(k-1)次,答案即为sum*k*n^(k-1)
技术分享
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<cctype>

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=(1<<29);
const double EPS=0.0000000001;
const double Pi=acos(-1.0);

int T;
int n;
ll k,p,a;

ll qpow(ll n,ll k)
{
    ll res=1;
    while(k){
        if(k&1) res=((res%p)*(n%p))%p;
        n=(n%p)*(n%p)%p;
        k>>=1;
    }
    return res;
}

int main()
{
    cin>>T;
    int tag=1;
    while(T--){
        cin>>n>>k>>p;
        ll sum=0;
        for(int i=1;i<=n;i++) scanf("%lld",&a),sum=(a%p+sum%p)%p;
        cout<<"Case "<<tag++<<": "<<sum*qpow(n,k-1)*(k%p)%p<<endl;
    }
    return 0;
}
View Code

 

light_oj 1213

标签:

原文地址:http://www.cnblogs.com/--560/p/4564806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!