Fantasy of a Summation (LightOJ - 1213)(快速幂+简单思维)
题解:根据题目给的程序,就是计算给的这个序列,进行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)(快速幂+简单思维)的更多相关文章
- Fantasy of a Summation LightOJ - 1213 (快速幂)
题意: 首先 只看第一层循环的A[0],是不是用了nk-1次 A[1]也是用了nk-1次······ 所以 第一层的sum(A[i]的和) 一共用了nk-1 所以第一层为sum * nk-1 因为又 ...
- LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)
http://lightoj.com/volume_showproblem.php?problem=1213 Fantasy of a Summation Time Limit:2000MS ...
- 解题报告:poj 3070 - 矩阵快速幂简单应用
2017-09-13 19:22:01 writer:pprp 题意很简单,就是通过矩阵快速幂进行运算,得到斐波那契数列靠后的位数 . 这是原理,实现部分就是矩阵的快速幂,也就是二分来做 矩阵快速幂可 ...
- POJ3070矩阵快速幂简单题
题意: 求斐波那契后四位,n <= 1,000,000,000. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
- hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)
题目 第一次做是看了大牛的找规律结果,如下: //显然我看了答案,循环节点是48,但是为什么是48,据说是高手打表出来的 #include<stdio.h> int main() { ], ...
- LightOj 1213 - Fantasy of a Summation(推公式 快速幂)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1213 #include <stdio.h> int cases, case ...
- 好的计数思想-LightOj 1213 - Fantasy of a Summation
https://www.cnblogs.com/zhengguiping--9876/p/6015019.html LightOj 1213 - Fantasy of a Summation(推公式 ...
- LightOJ1213 Fantasy of a Summation —— 快速幂
题目链接:https://vjudge.net/problem/LightOJ-1213 1213 - Fantasy of a Summation PDF (English) Statisti ...
- Fantasy of a Summation n个数,k层重复遍历相加。求它的和%mod的值;推导公式+快速幂
/** 题目:Fantasy of a Summation 链接:https://vjudge.net/contest/154246#problem/L 题意:n个数,k层重复遍历相加.求它的和%mo ...
随机推荐
- 【动态规划】Mathematical Curse
[来源]:2018年焦作网络赛B [题意]: 有n个数字,有m个符号运算.通过不回头(即选取m个数有顺序可言),消除巫术的,并达到最大的价值. 其实意思就是在数组里选取一段子序列,然后进行m次加减乘除 ...
- Neo4j WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual
you can add a line in /etc/default/neo4j: NEO4J_ULIMIT_NOFILE=60000 to set the ulimit setting (60000 ...
- java——包装类中的equals方法
基本数据类型包装类中的equals方法用于比对相同包装类中的值是否相等,如果两者比较的包装类类型不同则返回false: Byte public boolean equals(Object obj) { ...
- centos配置vsftpd服务2
ftp搭建 一.搭建前提a.ssh服务已经开启,b.防火墙关闭,c.连网1.查看ssh和防火墙的状态 service sshd status service iptables status 2.开启s ...
- python图像处理-生成随机验证码
前面说了PIL库,还说了图片的缩放.旋转和翻转.现在说下网站上常用的随机验证码的生成.参考网站:https://www.liaoxuefeng.com/wiki/1016959663602400/10 ...
- JavaJDBC【三、增删改查】
获取数据库连接后,可进行增删改查操作 语句生成: Statement s = con.createStatement(sql); //生成语句 PreparedStatement ps = (Prep ...
- mysql 关于字符串搜索的函数
与like作用相似 FIND_IN_SET LOCATE POSITION
- 鼠标悬停设置layui tips提示框
官方介绍:吸附层,灵活判断出现的位置,默认在元素的右侧弹出. layer.tips(content, follow, options) layer.tips(msg, '#id',{tips: 1}) ...
- Visual Studio 添加 自定义 路径宏
在编辑VS工程包含路径和库路径时,有时需要添加第三方包的路径,比如c++ boost库, 为了协作的方便,不合适直接把本地绝对路径添加入工程设置,此时可以添加自定义路径宏, 然后参与协作的每个开发人员 ...
- 数据总线&地址总线&控制总线
数据总线 (1) 是CPU与内存或其他器件之间的数据传送的通道. (2)数据总线的宽度决定了CPU和外界的数据传送速度. (3)每条传输线一次只能传输1位二进制数据.eg: 8根数据线一次可传送一个8 ...