http://lightoj.com/volume_showproblem.php?problem=1213

 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

 
根据题目上所附的代码不难看出题意,不再多说
公式   (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 = ;
const int INF = 0x3f3f3f3f;
typedef long long ll; int mod; ll Pow(int a, int b, int c)
{
ll ans = ;
a %= c;
while(b)
{
if(b % == )
ans = (ans * a) % c;
a = (a * a) % c;
b /= ;
}
return ans;
} int main()
{
int t, a[N], p = ;;
int n, k;
ll sum;
scanf("%d", &t);
while(t--)
{
p++;
sum = ;
scanf("%d%d%d", &n, &k, &mod);
for(int i = ; i < n ; i++)
{
scanf("%d", &a[i]);
sum += a[i];
}
ll s;
s = Pow(n, k - , mod);
s *= k;
sum %= mod;
sum *= s;
sum %= mod;
printf("Case %d: %lld\n", p, sum);
}
return ;
}
/*
3
2 4 3
1 30
4 9 5
22 18 2 22
2 2147483647 3333
2147483647 2147483647
*/
 

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

  1. 好的计数思想-LightOj 1213 - Fantasy of a Summation

    https://www.cnblogs.com/zhengguiping--9876/p/6015019.html LightOj 1213 - Fantasy of a Summation(推公式 ...

  2. LightOj 1213 - Fantasy of a Summation(推公式 快速幂)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1213 #include <stdio.h> int cases, case ...

  3. 1213 - Fantasy of a Summation

    1213 - Fantasy of a Summation         If you think codes, eat codes then sometimes you may get stres ...

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

    http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Me ...

  5. LightOJ1213 Fantasy of a Summation —— 快速幂

    题目链接:https://vjudge.net/problem/LightOJ-1213 1213 - Fantasy of a Summation    PDF (English) Statisti ...

  6. Fantasy of a Summation n个数,k层重复遍历相加。求它的和%mod的值;推导公式+快速幂

    /** 题目:Fantasy of a Summation 链接:https://vjudge.net/contest/154246#problem/L 题意:n个数,k层重复遍历相加.求它的和%mo ...

  7. Fantasy of a Summation (LightOJ - 1213)(快速幂+简单思维)

    题解:根据题目给的程序,就是计算给的这个序列,进行k次到n的循环,每个数需要加的次数是k*n^(k-1),所以快速幂取模,算计一下就可以了. #include <bits/stdc++.h> ...

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

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

  9. LightOj 1245 --- Harmonic Number (II)找规律

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1245 题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题 ...

随机推荐

  1. 并发服务器和HTTP协议

    单进程服务器 1. 完成一个简单的TCP服务器 from socket import * serSocket = socket(AF_INET, SOCK_STREAM) # 重复使用绑定的信息 se ...

  2. python3操作MySQL数据库

    安装PyMySQL 下载地址:https://pypi.python.org/pypi/PyMySQL 1.把操作Mysql数据库封装成类,数据库和表先建好 import pymysql.cursor ...

  3. Tessnet2 a .NET 2.0 Open Source OCR assembly using Tesseract engine

    http://www.pixel-technology.com/freeware/tessnet2/ Tessnet2 a .NET 2.0 Open Source OCR assembly usin ...

  4. leetcode814

    class Solution { public: TreeNode* pruneTree(TreeNode* root) { if(root==NULL) { return nullptr; } if ...

  5. fireDAC oracle

    copy 4 files to D:\oracleapp\Administrator\product\11.2.0\client_1\BIN setup win 64 bit client .down ...

  6. java中getAttribute和getParameter的区别

    getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 getParame ...

  7. 迷你MVVM框架 avalonjs 1.3.5发布

    本版本主要是修复内存泄漏问题,让其在移动端更好的运作. 修正visible BUG 详见这里 修正$fire方法里的正则错误 详见这里 修正ms-attr BUG,在IE9-11,直接用element ...

  8. js中创建table表格

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. Varint 数值压缩

    [Varint 数值压缩] Varint 是一种紧凑的表示数字的方法.它用一个或多个字节来表示一个数字,值越小的数字使用越少的字节数.这能减少用来表示数字的字节数.比如对于 int32 类型的数字,一 ...

  10. 每月IT摘录201807

    一.技术 1.专注于一个领域,横向扩展其他领域的技术.2.想得太多,做得太少. 3.想要成为一名web开发高手.必须熟悉以下内容: a.每次请求和响应的背后究竟发生了哪些步骤?客户端和服务器是如何通过 ...