1213 - Fantasy of a Summation
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 |
Output for Sample Input |
2 3 1 35000 1 2 3 2 3 35000 1 2 |
Case 1: 6 Case 2: 36 |
分析:由题得, 程序执行了n^k次加法, 每次取k个数, 并且每个数出现的次数是相等的为n^k * k/n次。
结合律
|
((a+b) mod p + c)mod p = (a + (b+c) mod p) mod p
((a*b) mod p * c)mod p = (a * (b*c) mod p) mod p
|
交换律
|
(a + b) mod p = (b+a) mod p
(a × b) mod p = (b × a) mod p
|
分配律
|
((a +b)mod p × c) mod p = ((a × c) mod p + (b × c) mod p) mod p
(a×b) mod c=(a mod c * b mod c) mod c
(a+b) mod c=(a mod c+ b mod c) mod c
(a-b) mod c=(a mod c- b mod c) mod c
|
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define N 11000
int mod;
ll qpow(int a, int b)
{
if(b == 0)
return 1;
ll tmp = qpow(a, b>>1);
tmp = tmp * tmp % mod;
if(b & 1)
tmp =(ll) (tmp * (a % mod) )% mod;
return tmp;
}
int main()
{
int T, cas;
int n, k;
int num[N];
scanf("%d", &T);
cas = 0;
while(T--)
{
cas++;
scanf("%d%d%d", &n, &k, &mod);
ll sum = 0;
for(int i = 0; i < n; i++)
{
scanf("%d", &num[i]);
sum = (sum + num[i] % mod) % mod;
}
ll ans = qpow(n, k-1);
ll cnt = (ans % mod * k % mod * sum % mod) % mod;
printf("Case %d: %lld\n", cas, cnt);
}
return 0;
}
1213 - Fantasy of a Summation的更多相关文章
- LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)
http://lightoj.com/volume_showproblem.php?problem=1213 Fantasy of a Summation Time Limit:2000MS ...
- 好的计数思想-LightOj 1213 - Fantasy of a Summation
https://www.cnblogs.com/zhengguiping--9876/p/6015019.html LightOj 1213 - Fantasy of a Summation(推公式 ...
- LightOj 1213 - Fantasy of a Summation(推公式 快速幂)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1213 #include <stdio.h> int cases, case ...
- 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 ...
- Fantasy of a Summation (LightOJ - 1213)(快速幂+简单思维)
题解:根据题目给的程序,就是计算给的这个序列,进行k次到n的循环,每个数需要加的次数是k*n^(k-1),所以快速幂取模,算计一下就可以了. #include <bits/stdc++.h> ...
- Fantasy of a Summation LightOJ - 1213 (快速幂)
题意: 首先 只看第一层循环的A[0],是不是用了nk-1次 A[1]也是用了nk-1次······ 所以 第一层的sum(A[i]的和) 一共用了nk-1 所以第一层为sum * nk-1 因为又 ...
- [kuangbin带你飞]专题十四 数论基础
ID Origin Title 111 / 423 Problem A LightOJ 1370 Bi-shoe and Phi-shoe 21 / 74 Problem B ...
- KUANGBIN带你飞
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题 //201 ...
随机推荐
- kafka模式对比
Receiver是使用Kafka的高层次Consumer API来实现的.receiver从Kafka中获取的数据都是存储在Spark Executor的内存中的,然后Spark Streaming启 ...
- Mysql连接出现时区问题
错误提示: The server time zone value '¥x¥_¼Ð·Ç®É¶¡' is unrecognized or represents more than one time zon ...
- CocoaPods中的头文件import导入时不能自动补齐的解决方法
1.选择target(就是左边你的工程target)-->BuildSettings-->search Paths下的User Header Search Paths 2.添加“$(POD ...
- 修改现有消息类让.net core项目支持Protobuf - 【无需使用 [ProtoBuf.ProtoContract] 的方法】
前言 第二次发博客,希望大家多多鼓励!!! 又接无上老板的一个需求,需要让.net core消息发送端跟消息接收端通信的消息是protobuf格式的(基于protobuf比json小一倍数据量,独特的 ...
- 从0到1掌握某Json-TemplatesImpl链与ysoserial-jdk7u21的前因后果
本文首发于先知社区: https://xz.aliyun.com/t/7096 前言 作为一名安全研究人员(java安全菜鸡),知道拿到exp怎么打还不够,还得进一步分析exp构造原理与漏洞原理才行. ...
- Dart语言学习( 一) 为什么学习Dart?
为什么学习Dart? Google及全球的其他开发者,使用 Dart 开发了一系列高质量. 关键的 iOS.Android 和 web 应用. Dart 非常适合移动和 web 应用的开发. 高效 D ...
- xsd 和 wsdl
xsd : 可用方便 不同的语言之间的 用命令行来 转换对应语言的. wsdl: 可用方便不同语言的类描述 用命令行 来相互转换. 类似 thift me ?
- Hbase与Maven工程的Spring配置笔记
1.HBase基本操作 hbase shell: 连接到正在运行的HBase实例 help: 显示一些基本的使用信息以及命令示例. 需要注意的是: 表名, 行, 列都必须使用引号括起来 create ...
- 《阿里巴巴Java开发手册》码出高效详解(一)- 为什么要学习阿里编码手册
<Java 开发手册>(以下简称<手册>)是每个 Java 工程师人手必备的一本参考指南.该手册包括 编程规约.异常日志.单元测试.安全规约.MySQL 数据库.工程结构.设计 ...
- epel-release的卸载重装
1.yum remove epel-release 2.清空epel目录:rm -rf /var/cache/yum/x86_64/6/epel/ 3.安装,yum install epel-rel ...