51nod1103 N的倍数
【题解】
先预处理出模N意义下的前缀和sum[i]。
1.如果sum[i]=0,那么1~i的数之和就是N的倍数
2.sum[i]%N总共有0~N-1这N种情况;根据1,如果sum[i]为0则必定有解;如果不存在sum[i]=0,那么根据抽屉原理,有N个前缀和,N-1种情况,那么一定存在sum[i]=sum[j],那么i+1~j的数之和就是N的倍数
由上可知,一定存在一种方案满足取出连续的一些数使得这些数的和是N的倍数
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int n,a[maxn],sum[maxn],last[maxn];
void read(int &k){
k=; int f=; char c=getchar();
while (c<''||c>'')c=='-'&&(f=-),c=getchar();
while (''<=c&&c<='')k=k*+c-'',c=getchar();
k*=f;
}
int main(){
read(n);
for (int i=;i<=n;i++) read(a[i]),sum[i]=(sum[i-]+a[i])%n;
for (int i=;i<=n;i++){
if (sum[i]==){
printf("%d\n",i);
for (int j=;j<=i;j++) printf("%d\n",a[j]);
return ;
}
else if (last[sum[i]]){
printf("%d\n",i-last[sum[i]]);
for (int j=last[sum[i]]+;j<=i;j++) printf("%d\n",a[j]);
return ;
}
last[sum[i]]=i;
}
}
51nod1103 N的倍数的更多相关文章
- [转]倍数提高工作效率的 Android Studio 奇技
转自:http://android.jobbole.com/81687/ 倍数提高工作效率的 Android Studio 奇技 2015/10/08 · 技术分享 · 4 评论· Android S ...
- 51nod 1109 01组成的N的倍数
用01 组成 N的最小倍数 这个BFS搜索就好. 类似这道: ZOJ Problem Set - 1530 每次 要么是0 要么是1, 记入余数,和前驱. #include<bits/stdc ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Codeforces Round #276 (Div. 1) B. Maximum Value 筛倍数
B. Maximum Value Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/prob ...
- Oracle PL/SQL 找出100以内是3和5的倍数的数 循环语句
循环: loop --执行代码 exit when 表达式;--当表达式为真退出循环.(注意,其编写位置决定循环为先判断还是先执行,相当于java的while或do-while) end loop; ...
- 用if else 判断是不是7的倍数等
static void Main(string[] args) { while (true) { int b; ...
- NYOJ--517--最小公倍数(大数打表)
最小公倍数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致. 但也并非纯粹的偶然:60是个优秀的数字 ...
- 百度之星2017初赛A轮 1001 小C的倍数问题
小C的倍数问题 Accepts: 1990 Submissions: 4931 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- 51nod 1103 N的倍数 思路:抽屉原理+前缀和
题目: 这是一道很神奇的题目,做法非常巧妙.巧妙在题目要求n个数字,而且正好要求和为n的倍数. 思路:用sum[i]表示前i个数字的和%n.得到sum[ 1-N ]共N个数字. N个数字对N取模,每个 ...
随机推荐
- Codeforces Round #273 (Div. 2)D. Red-Green Towers DP
D. Red-Green Towers There are r red and g green blocks for construction of the red-green tower. Re ...
- 让mongodb执行js文件
环境: Linux js代码: 循环删除表中的数据: clear-mongodb-dialog.js print('=========BEGIN=========='); for(var i of [ ...
- poj3254Corn Fields
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13765 Accepted: 7232 Desc ...
- bzoj 4590: [Shoi2015]自动刷题机
好恶心.. 二分上界到100000LL*1000000000LL %_% #include<cstdio> #include<iostream> #include<cs ...
- IDEA Spark程序报错处理
错误一: // :: ERROR Executor: Exception ) java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Prod ...
- 46. Ext中namespace的作用(转)
转自:https://www.cnblogs.com/givemeanorange/p/5569954.html Ext中在每一个页面中添加一个namespace呢,就像下面的代码: // creat ...
- html5: table表格与页面布局整理
传统表格布局之table标签排版总结: 默认样式: <style> table { max-width: 800px; border-spacing: 2px; border-coll ...
- JavaScript学习四
2019-06-01 09:09:23 坚持,加油!!! 函数的学习 <html> <head> <script type="text/javascript&q ...
- yield from (python生成器)
#生成器中的yield from是干什么用的(一般多用于线程,协程那)def func(): # for i in 'AB': # yield i yield from 'AB' # 就相当于上面的f ...
- hbuilder中的 http://www.w3.org/TR/html4/frameset.dtd
<!-- This is the HTML 4.01 Frameset DTD, which should be used for documents with frames. This DTD ...