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

题意:

首先 只看第一层循环的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;
}

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

时间: 2024-10-21 09:57:30

Fantasy of a Summation LightOJ - 1213 (快速幂)的相关文章

快速幂——L - Fantasy of a Summation LightOJ - 1213

L - Fantasy of a Summation LightOJ - 1213 快速幂,每个元素的次数k*n^(k-1),模完了加就行.要记得在每个位置都模一下 1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <map> 5 #include <set> 6 #include <algorithm> 7 #include <f

LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)

http://lightoj.com/volume_showproblem.php?problem=1213 Fantasy of a Summation Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1213 Description If you think codes, eat codes then sometimes you

快速幂 E - Leading and Trailing LightOJ - 1282

E - Leading and Trailing LightOJ - 1282 快速幂主要是把n拆成2进制位,如果这一位有那么就乘,没有就不乘,而计数器也就是x是不断推进的,从x->x^2->x^4直到n的最高位精髓在于取模,后一步的要求结果只与前一步的模后数据有关 . 对于后三个数用了log10.log函数对求n^k这种问题还是很有用的.没想出来. 1 #include <iostream> 2 #include <cstring> 3 #include <st

LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)

题目链接:LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 得到递推式之后就是矩阵快速幂了 注意:模2^64,定义成unsigned long long 类型,因为无符号类型超过最大范围的数与该数%最大范围 的效果是一样的. AC代码: #include<stdio.h> #include<string.h> #define LL unsigned long long struct Ma

LightOJ 1132 - Summing up Powers 矩阵快速幂+排列组合

链接:http://lightoj.com/volume_showproblem.php?problem=1132 1132 - Summing up Powers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given N and K, you have to find (1K + 2K + 3K + ... + NK) % 232 Input Input starts with an i

HDU - 6027 Easy Summation(快速幂)解题

Easy Summation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1107    Accepted Submission(s): 443 Problem Description You are encountered with a traditional problem concerning the sums of po

LightOJ 1070 - Algebraic Problem 矩阵快速幂

题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessar

hdu6027Easy Summation(快速幂取模)

Easy Summation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 4112    Accepted Submission(s): 1676 Problem Description You are encountered with a traditional problem concerning the sums of po

LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)

链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk. 思路: 后三位快速幂取余,考虑前三位. \(n^k\)可以表示为\(a*10^m\)即使用科学计数法. 对两边取对数得到\(k*log