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

Fantasy of a Summation LightOJ - 1213 (快速幂)

时间:2018-06-16 13:34:36      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:cin   ios   #define   TE   class   inf   while   \n   space   

题意:

首先 只看第一层循环的A[0],是不是用了nk-1次  A[1]也是用了nk-1次······ 所以 第一层的sum(A[i]的和) 一共用了nk-1 所以第一层为sum * nk-1

因为又k层循环 所以全部为sum * nk-1 * k

最后不要忘了 % MOD

代码如下:

 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;
typedef long long LL;
LL MOD, n, k;
LL init(LL a, LL b)
{
    LL res = 1;
    while(b)
    {
        if(b & 1 ) res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

int main()
{
    int T;
    cin>> T;
    int kase = 0;
    while(T--)
    {
        cin>> n >> k >> MOD;
        LL sum = 0;
        for(int i=0; i<n; i++){
            LL temp;
            cin>> temp;
            sum = (sum + temp) % MOD; 
        }
        printf("Case %d: %lld\n",++kase,sum*init(n,k-1)  * k%MOD);

    }    
    
    
    return 0;
}

 

Fantasy of a Summation LightOJ - 1213 (快速幂)

标签:cin   ios   #define   TE   class   inf   while   \n   space   

原文地址:https://www.cnblogs.com/WTSRUVF/p/9190280.html

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