题目链接: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. 经典收藏 50个jQuery Mobile开发技巧集萃

    http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 1.Backbone移动实例 这是在Safari中运行的一款Ba ...

  2. C# 格式化字符串,日期,字符串操作汇总

    时间格式化 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2005-6-6 14:33:34 如果要换成成200506,06-2005,2005-6-6或更多的该怎么办呢 我们要用到:D ...

  3. 厉害了,摩托罗拉发布全球首款支持VR和AR的手机MotoZ

    目前支持谷歌daydream移动VR生态系统的手机型号并不多,除了谷歌自家的Pixel系列外,还有华为推出的mate9系列.如今又到了一位新成员,即升级到Android 7.0后的Moto Z. 更为 ...

  4. javac命令出现“**.java使用了未经检查或不安全的操作”

    Collection col=new ArrayList();引发了“**.java使用了未经检查或不安全的操作”错误, 这是因为JDK1.5中引进了泛型,但是你的ArrayList却没有采用,所有会 ...

  5. hdu1181 变形课

    Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个 ...

  6. into outfile 生成sql脚本

    select concat('insert into t_dm_stage(STAGE_ID,STAGE_NAME) values(',STAGE_ID,',','\'',STAGE_NAME,'\' ...

  7. wamp环境下安装memcached最好的详解教程^.^:(只需要3个步骤 )

    win8.1 wampserver2.5 -Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b 可以参考一下部分讲解有图,我就是看4-5个讲解,结合有一篇的截图最终才搞定的 ...

  8. Android -- 服务组件的使用(1)

    1. 效果图

  9. SQL笔记----在一个关系表中操作列

    使用alter关键字,可以为一个表添加新的列. 比如: 给Persons的表中添加一列,名字为Birthday,类型是date. ALTER TABLE Persons ADD Birthday da ...

  10. JavaScript笔记——引用类型之Object类型和Function类型

    <JavaScript高级程序设计>中介绍的几种JavaScript的引用类型,本文只记了Object跟Function类型 Object类型 创建对象 var person = new ...