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)要用快数幂来求解
为了防止溢出就尽情地去取余吧。。。
 
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int N = ;
  10. const int INF = 0x3f3f3f3f;
  11. typedef long long ll;
  12.  
  13. int mod;
  14.  
  15. ll Pow(int a, int b, int c)
  16. {
  17. ll ans = ;
  18. a %= c;
  19. while(b)
  20. {
  21. if(b % == )
  22. ans = (ans * a) % c;
  23. a = (a * a) % c;
  24. b /= ;
  25. }
  26. return ans;
  27. }
  28.  
  29. int main()
  30. {
  31. int t, a[N], p = ;;
  32. int n, k;
  33. ll sum;
  34. scanf("%d", &t);
  35. while(t--)
  36. {
  37. p++;
  38. sum = ;
  39. scanf("%d%d%d", &n, &k, &mod);
  40. for(int i = ; i < n ; i++)
  41. {
  42. scanf("%d", &a[i]);
  43. sum += a[i];
  44. }
  45. ll s;
  46. s = Pow(n, k - , mod);
  47. s *= k;
  48. sum %= mod;
  49. sum *= s;
  50. sum %= mod;
  51. printf("Case %d: %lld\n", p, sum);
  52. }
  53. return ;
  54. }
  55. /*
  56. 3
  57. 2 4 3
  58. 1 30
  59. 4 9 5
  60. 22 18 2 22
  61. 2 2147483647 3333
  62. 2147483647 2147483647
  63. */
 

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. 关于SSE的一些资料

    之前查到的,觉得还不错,一直没时间看,放这里先存着 https://software.intel.com/en-us/articles/using-intel-streaming-simd-exten ...

  2. Cmder的安装

    Cmder把conemu,git-for-windows和clink打包在一起,让你无需配置就能使用一个真正干净的Linux终端!性感的外观,强大的功能!代替了Windows原生的Cmd 1. 安裝 ...

  3. c# 数据集调试工具插件

    DataSetSpySetup,调试期查看dataset数据集的记录内容, Debug DataSet

  4. open方法读写文件

    vb使用open方法读写文件 (一)打开和关闭文件 1.顺序文件 打开顺序文件,我们可以使用Open语句.它的格式如下: Open pathname For [Input |Output |Appen ...

  5. nginx错误页面重定向

    一.Nginx错误页面优雅显示的原因?   当我们访问网站时,由于特殊的原因,经常会出现诸如403,404,503等错误,这极大的影响用户的访问体验,所以我们很有必要做一下错误页面的优雅显示,以提升用 ...

  6. k8s实战

    wget https://github.com/coreos/etcd/releases/download/v2.2.0/etcd-v2.2.0-linux-amd64.tar.gz etcd -na ...

  7. ORA-01157:无法标识/锁定数据文件,ORA-01110:数据文件。。。

  8. Android开发之通过包管理器获取安装应用信息

    最近在自己写一个APP,有一个模块需要获取手机应用的一些信息.坑还是有,但都基本踩过了,自己把他实现了出来,实现方法还是很需要掌握的.底部弹出的对话框中四个选项的实现不多做说明,主要讲讲如何获取这些安 ...

  9. 40. Combination Sum II (Back-Track)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  10. shader一般都是用工具调试的

    N卡的话用nvidia的nVidia FX Composer, A卡的话用ATI的render monkey 顶点着色器从何方拿到这些数据?在U3D环境下,答案是从绑定到game object中的Me ...