题目链接: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. SqlServer 事务回滚(1)

    SQL事务   一.事务概念     事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是一个不可分割的工作逻辑单元.在数据库系统上执行并发操作 ...

  2. BZOJ3548 : [ONTAK2010]Party

    首先将朋友通过并查集缩起来,因为$P\geq\frac{n(n-1)}{3}$,所以最后最多剩下$46$个点. 将自相矛盾的点删掉,就变成求最大权独立集问题,这等于求补图的最大团. 然后直接用Bron ...

  3. Angular JS中的依赖注入

    依赖注入DI angularjs中与DI相关有angular.module().angular.injector(). $injector.$provide. DI 容器3要素:服务的注册.依赖关系的 ...

  4. Spring MVC Rest 支持CORS

    新建cors filter文件, package cn.ac.iscas.pebble.ufe.tools; import java.io.IOException; import javax.serv ...

  5. sublime注释插件与javascript注释规范

    前言 代码中注释是不可少的,即使是自己写的代码,过了一段时间之后再重看,如果没有注释记录的话,可能会想不到当初是这样实现的,尤其是在业务逻辑比较复杂的项目,注释变得尤为重要.怎么优雅的写有用的注释呢? ...

  6. 快速搭建IE测试环境(Virtualbox+ievms)

    IE下的测试 作为一个有追求的程序员,应该尽可能的远离Windows系统.不论从专业开发者的角度,还是仅仅作为最终用户从使用体验上来说,Windows都可以算是垃圾中的战斗机: 没有shell . 响 ...

  7. 【BZOJ】3456: 城市规划

    http://www.lydsy.com/JudgeOnline/problem.php?id=3456 题意:求n个点的无向连通图的方案.(n<=130000) #include <bi ...

  8. 【BZOJ2821】作诗(Poetize) 分块

    Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次, ...

  9. [CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数

    18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the ...

  10. [CareerCup] 16.4 A Lock Without Deadlocks 无死锁的锁

    16.4 Design a class which provides a lock only if there are no possible deadlocks. 有很多方法可以避免死锁的发生,一个 ...