题目链接:http://lightoj.com/volume_showproblem.php?problem=1213

#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;
}

  

题意:告诉你这段代码,然后优化,求res; n (1 ≤ n ≤ 1000), K (1 ≤ K < 231), MOD (1 ≤ MOD ≤ 35000)

我们很容易就知道最内成的加法式子执行了n^K次,每次加了K个数,所以一共加了K*n^K个数,一共有n个数,每个数加的次数一定是相同的,所以每个数都加了K*n^(K-1)次,所以结果就是Sum*K*n^(K-1)%mod; 快速幂求一下即可;

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
using namespace std;
typedef long long LL;
const int oo = 0xfffffff;
const int N = 1e3+5; LL K, mod, a[N]; int n; LL Pow(LL a, LL b)
{
LL ans = 1;
while(b)
{
if(b&1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
} int main()
{
int T, t = 1; scanf("%d", &T); while(T--)
{ scanf("%d %lld %lld", &n, &K, &mod); LL sum = 0; for(int i=1; i<=n; i++)
{
scanf("%lld", &a[i]); sum = (sum + a[i])%mod;
} sum = (sum * K)%mod * Pow((LL)n, K-1)%mod; printf("Case %d: %lld\n", t++, sum);
}
return 0;
}

  

LightOj 1213 - Fantasy of a Summation(推公式 快速幂)的更多相关文章

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

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

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

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

  3. 1213 - Fantasy of a Summation

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

  4. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

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

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

  7. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

  8. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

  9. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

随机推荐

  1. Poj1131-Octal Fractions

    Octal Fractions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6669   Accepted: 3641 D ...

  2. ural 1250. Sea Burial

    1250. Sea Burial Time limit: 1.0 secondMemory limit: 64 MB There is Archipelago in the middle of a s ...

  3. BZOJ 2626 & KDtree

    题意: 二维平面n个点 每次给出一个点询问距离第k小的点. SOL: kdtree裸题,抄了一发别人的模板...二维割起来还是非常显然的.膜rzz的论文. 不多说了吧.... Code: /*==== ...

  4. BZOJ 1468 & 点分治

    题意: 带权树,求距离小于k的点对数目. SOL: 参考http://blog.csdn.net/jiangshibiao/article/details/25738041解决了题意问题... 代码是 ...

  5. Win10 利用安装盘启用 .NET Framework 3.5

    以管理员身份运行命令提示符,在“管理员:命令提示符”窗口中输入以下命令:dism.exe /online /enable-feature /featurename:netfx3 /Source:D:\ ...

  6. Codeforces Round #243 (Div. 2) B. Sereja and Mirroring

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  7. IOS 蓝牙相关-连接外设的代码实现(2)

    我们具体说明一下中心模式的应用场景.主设备(手机去扫描连接外设,发现外设服务和属性,操作服务和属性的应用.一般来说,外设(蓝牙设备,比如智能手环之类的东西), 会由硬件工程师开发好,并定义好设备提供的 ...

  8. C语言(3)

    C语言(3)----数据输入 输入时的关键字为scanf.如我们要从键盘上输入一个数,放在变量a里面,则可以写成scanf("%d",&a); "&&qu ...

  9. 初识GO语言——安装Go语言

    本文包括:1)安装Go语言.2)运行第一个Go语言.3)增加vim中对Go语言的高亮支持. 1.安装Go语言 本文采用源码安装Go语言,Go语言的源代码在百度网盘 http://pan.baidu.c ...

  10. C#读取数据库字节流生成图片

    前台用DataList绑定 <asp:DataList ID="DataList1" runat="server"> <ItemTemplat ...