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

#include <bits/stdc++.h>

using  namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
long long pow_mod(ll a, ll k, ll mod)
{
ll ans = 1;
while(k)
{
if(k%2)
ans *= a;
ans %= mod;
a = a * a;
a %= mod;
k /=2;
}
return ans;
}
int main()
{
int T;
ll n,k,mod,x,sum;
while(~scanf("%d",&T))
{
int cas = 1;
while(T--)
{
sum = 0;
scanf("%lld%lld%lld",&n,&k,&mod);
for(ll i = 0; i < n; i ++)
{
scanf("%lld",&x);
sum += (x * (k * pow_mod(n,k-1,mod)%mod)%mod);
sum %= mod;
}
printf("Case %d: %lld\n",cas++, sum);
}
}
return 0;
}

Problem:

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 n, K, MOD 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

Fantasy of a Summation (LightOJ - 1213)(快速幂+简单思维)的更多相关文章

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

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

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

    http://lightoj.com/volume_showproblem.php?problem=1213  Fantasy of a Summation Time Limit:2000MS     ...

  3. 解题报告:poj 3070 - 矩阵快速幂简单应用

    2017-09-13 19:22:01 writer:pprp 题意很简单,就是通过矩阵快速幂进行运算,得到斐波那契数列靠后的位数 . 这是原理,实现部分就是矩阵的快速幂,也就是二分来做 矩阵快速幂可 ...

  4. POJ3070矩阵快速幂简单题

    题意:       求斐波那契后四位,n <= 1,000,000,000. 思路:        简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...

  5. hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)

    题目 第一次做是看了大牛的找规律结果,如下: //显然我看了答案,循环节点是48,但是为什么是48,据说是高手打表出来的 #include<stdio.h> int main() { ], ...

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

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

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

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

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

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

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

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

随机推荐

  1. No compiler is provided in this environment. Perhaps you are running on a JR

    maven编译项目时出错,提示信息如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3 ...

  2. dva中的一些备忘

    dva/router就是react-router-dom dva/router里的routerRedux就是react-router-redux 一个react的单页面应用: 编写一个基础框架,包含单 ...

  3. es6的学习

    前端语言真的是比较多,尤其是做项目的时候,为了节约项目开发时间,框架选型一般选择比较成熟的,上手比较快捷的.目前领域里比较火的是vue,angular,以及react.为了逼迫自己好好学习.先从vue ...

  4. Delphi最新的路线图

    没想到,官方又发布了一个路线图,同上一个相比,多了10.3.3版本,而其中,最主要的iOS 13支持,即下半年发布的新的苹果系统,再一个就是android 64的delphi支持!年底前发布,够官忙伙 ...

  5. web录音——上传录音文件

    捕获麦克风 一.  前言    公司项目需要实现web录音,刚刚好接手此功能,由于之前未接触过,在网上找了些资料做对比 )   https://www.cnblogs.com/starcrm/p/51 ...

  6. MySQL8.x msi版安装教程

    一.下载MySQL 官网下载地址 https://dev.mysql.com/downloads/windows/installer/8.0.html  下载第二个即可(虽然只有32位的 但是会同时安 ...

  7. 《python解释器源码剖析》第2章--python中的int对象

    2.0 序 在所有的python内建对象中,整数对象是最简单的对象.从对python对象机制的剖析来看,整数对象是一个非常好的切入点.那么下面就开始剖析整数对象的实现机制 2.1 初识PyLongOb ...

  8. Java面向对象(三) 【面向对象深入:抽象类,接口,内部类等】

    面向对象(Object Oriented) 1.抽象类抽象就是将拥有共同方法和属性的对象提取出来.提取后,重新设计一个更加通用.更加大众化的类,就叫抽象类.1)abstract 关键字修饰类.方法,即 ...

  9. C++ 批量打开写入文件

    用到了C++17的filesystem 库 说明:这个函数主要是用来处理日志中不同Thread的日志,主要目的是将不同Thread的日志写到不同的文件中 int GetThreadTime(const ...

  10. PAT Basic 1092 最好吃的月饼 (20 分)

    月饼是久负盛名的中国传统糕点之一,自唐朝以来,已经发展出几百品种. 若想评比出一种“最好吃”的月饼,那势必在吃货界引发一场腥风血雨…… 在这里我们用数字说话,给出全国各地各种月饼的销量,要求你从中找出 ...