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

根据题目上所附的代码不难看出题意,不再多说

公式   (sum*n^(k - 1) * k) % mod

好不容易推出公式,结果才取余上一直出错,郁闷半天!!!!

n^(k - 1)要用快数幂来求解

为了防止溢出就尽情地去取余吧。。。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

const int N = 1010;
const int INF = 0x3f3f3f3f;
typedef long long ll;

int mod;

ll Pow(int a, int b, int c)
{
   ll ans = 1;
   a %= c;
   while(b)
   {
       if(b % 2 == 1)
        ans = (ans * a) % c;
       a = (a * a) % c;
       b /= 2;
   }
    return ans;
}

int main()
{
    int t, a[N], p = 0;;
    int n, k;
    ll sum;
    scanf("%d", &t);
    while(t--)
    {
        p++;
        sum = 0;
        scanf("%d%d%d", &n, &k, &mod);
        for(int i = 0 ; i < n ; i++)
        {
            scanf("%d", &a[i]);
             sum += a[i];
        }
        ll s;
        s = Pow(n, k - 1, mod);
        s *= k;
        sum %= mod;
        sum *= s;
        sum %= mod;
        printf("Case %d: %lld\n", p, sum);
    }
    return 0;
}
/*
3
2 4 3
1 30
4 9 5
22 18 2 22
2 2147483647 3333
2147483647 2147483647
*/
时间: 2024-08-16 13:51:41

LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)的相关文章

LightOJ 1282 Leading and Trailing (快数幂 + 数学)

http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1282 Description You are given two integers: n and k, your task is t

快速幂——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

E - Fantasy of a Summation LightOJ1213

E - Fantasy of a Summation Time Limit: 2000/1000 MS (Java/Others)      Memory Limit: 128000/64000 KB (Java/Others) Submit Status Problem Description If you think codes, eat codes then sometimes you may get stressed. In your dreams you may see huge co

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

LightOJ 13361336 - Sigma Function (找规律 + 唯一分解定理)

http://lightoj.com/volume_showproblem.php?problem=1336 Sigma Function Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1336 Description Sigma function is an interesting function in Number Theor

LightOJ - 1245 Harmonic Number (II) (找规律)

题目大意:给出一个n(1 <= n < 2^31)求出H(n)的结果,H(n)的定义为下: 分析:对于一个n,设 t = n / i: 满足 t >= 1的有多少个呢? 有 n / 1 个. 满足 t >= 2的有多少个呢? 有 n / 2 个. -- 满足 t >= k的有多少个呢? 有 n / k 个. 以上结论不难发现,我们再进一步就能发现: 满足 t == 1 的有 n/1 - n/2 个 满足 t == 2 的有 n/2 - n/3 个 -- 满足 t == k 的

HDU 4990 Reading comprehension (找规律+矩阵快速幂)

题目链接:HDU 4990 Reading comprehension 题目给的一个程序其实就是一个公式:当N=1时 f[n]=1,当n>1时,n为奇数f[n]=2*f[n-1]+1,n为偶数f[n]=2*f[n-1]. 先不取模,计算前十个找规律.得到一个递推公式:f[n]=2*f[n-2]+f[n-1]+1 然后快速幂解决之. 给出一个神奇的网站(找数列通项):http://oeis.org/ AC代码: #include<stdio.h> #include<string.h&

vijos - P1447开关灯泡 (大数模板 + 找规律 + 全然数 + python)

P1447开关灯泡 Accepted 标签:CSC WorkGroup III[显示标签] 描写叙述 一个房间里有n盏灯泡.一開始都是熄着的,有1到n个时刻.每一个时刻i,我们会将i的倍数的灯泡改变状态(即原本开着的现将它熄灭,原本熄灭的现将它点亮),问最后有多少盏灯泡是亮着的. 格式 输入格式 一个数n 输出格式 m,表示最后有m盏是亮着的 例子1 例子输入1[复制] 5 例子输出1[复制] 2 限制 1s 提示 范围:40%的数据保证,n<=maxlongint 100%的数据保证,n<=

hiho_41周_骨牌覆盖一_招规律+矩阵快速幂

题目 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个2xN的长条形棋盘,然后用1x2的骨牌去覆盖整个棋盘.对于这个棋盘,一共有多少种不同的覆盖方法呢? 举个例子,对于长度为1到3的棋盘,我们有下面几种覆盖方式: 输入 第1行:1个整数N.表示棋盘长度.1≤N≤100,000,000 输出 第1行:1个整数,表示覆盖方案数 MOD 19999997 样例输入 62247088 样例输出 17748018         前面几组一写,,很容易就能发现规律,是一个线性递推,甚至