poj2356Find a multiple——鸽巢定理运用
题目:http://poj.org/problem?id=2356
N个数,利用鸽巢定理可知应有N+1个前缀和(包括0),因此其%N的余数一定有重复;
同余的两个前缀和之差一定为N的倍数,据此得出答案。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,a[10005];
long long mod[10005],s;
int main()
{
scanf("%d",&n);
memset(mod,-1,sizeof mod);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s+=a[i];
if(s%n==0)
{
printf("%d\n",i);
for(int j=1;j<=i;j++)
printf("%d\n",a[j]);
return 0;
}
if(mod[s%n]==-1)mod[s%n]=i;
else
{
printf("%d\n",i-mod[s%n]);
for(int j=mod[s%n]+1;j<=i;j++)
printf("%d\n",a[j]);
return 0;
}
}
printf("0");
return 0;
}
poj2356Find a multiple——鸽巢定理运用的更多相关文章
- HDU 5776 sum( 鸽巢定理简单题 )
链接:传送门 题意:给一个长为 n 的串,问是否有子串的和是 m 的倍数. 思路:典型鸽巢定理的应用,但是这里 n,m 的大小关系是不确定的,如果 n >= m 根据定理可以很简单的判定是一定有 ...
- [POJ2356] Find a multiple 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8776 Accepted: 3791 ...
- [poj2356]--Find a multiple ——鸽巢原理
题意: 给定n个数,从中选取m个数,使得\(\sum | n\).本题使用Special Judge. 题解: 既然使用special judge,我们可以直接构造答案. 首先构造在mod N剩余系下 ...
- POJ 2356 Find a multiple( 鸽巢定理简单题 )
链接:传送门 题意:题意与3370类似 注意:注意输出就ok,输出的是集合的值不是集合下标 /***************************************************** ...
- codeforces 851C Five Dimensional Points(鸽巢原理)
http://codeforces.com/contest/851/problem/C 题意 - 给出 n 个五维空间的点 - 一个点a为 bad 的定义为 存在两点 b, c, 使的<ab, ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- poj 2356 Find a multiple(鸽巢原理)
Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...
- POJ2356 Find a multiple 抽屉原理(鸽巢原理)
题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...
- [POJ2356]Find a multiple 题解(鸽巢原理)
[POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...
随机推荐
- java提高同步锁的几点建议
1.减少锁的持有时间,只对关键的代码块加锁,减少synchronized锁内部的无关模块: 2.减小锁粒度,如Collections.synchronizedMap(map)返回线程安全的map会锁整 ...
- CISCO Configuration Examples and TechNotes
from: http://www.cisco.com/c/en/us/tech/ip/ip-routing/tech-configuration-examples-list.html Border ...
- window+Jira+SQL Server
window下Jira+SQL Server部署+汉化+破解(亲测2018.5) 网上很多都是jira+mysql部署的文章,由于我现在有需求要用SQL Server数据库,因此就动手试了一下,参考网 ...
- 【PyCharm编辑器】之无法导入引用手动新建的包或类,报:This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases.
一.现象描述 如下图所示,手动新建个类包calculator.py,想在test.py文件引用它,发现一直报红线,引用失败 Unresolved reference 'calculator' less ...
- load-on-startup 解释
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" &qu ...
- hadoop 相关工具访问端口(转)
原文:http://www.tuicool.com/articles/BB3eArJ hadoop系统部署时用到不少端口.有的是Web UI所使用的,有的是内部通信所使用的,有的是监控所使用的.实际系 ...
- WPF自定义选择年月控件详解
本文实例为大家分享了WPF自定义选择年月控件的具体代码,供大家参考,具体内容如下 封装了一个选择年月的控件,XAML代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- 在另一个线程中无法用((CMainFrame *)AfxGetMainWnd())
一个vc6的项目放到vc8下重新编译这里死活过不去 查了些资料无果后来翻到一句老外的回答 If AfxGetMainWnd is called from the application’s prima ...
- [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- GS(道具,帮会)定时存储
//最近数据库存储做了重大改变,数据库内部的回头再说,先看看GS这边的 .现在感觉数据库的状态将请求包放入命令队列中,以前是全部放进去,这样让其他的数据库操作不会随着数据库定时器而变慢,GS线程去驱动 ...