【题解】LightOJ1213 Fantasy of a Summation 快速幂

题目链接
这里写图片描述

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


根据给出的代码推个公式就行了。老是有一种错觉,不会爆int……于是暴躁的搜索int替换成long long,然后就出现了long long main()(大雾)

#include<cstdio>
long long n,k,mod,a;
long long t;
long long qpow(long long a,long long b)
{
    long long ret=1;
    for(;b;b>>=1)
    {
        if(b&1)ret=ret*a%mod;
        a=a*a%mod;
    }
    return ret;
}
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%lld",&t);
    for(long long ca=1;ca<=t;ca++)
    {
        scanf("%lld%lld%lld",&n,&k,&mod);
        long long sum=0;
        for(long long i=1;i<=n;i++)
        {
            scanf("%lld",&a);
            sum=(sum+a)%mod;
        }
        printf("Case %lld: %lld\n",ca,((sum*k%mod)*qpow(n,k-1)%mod));
    }
    return 0;
}

总结

推公式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值