poj2356 Find a multiple
/*
POJ-2356 Find a multiple ----抽屉原理
Find a multiple
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4228 Accepted: 1850 Special Judge
Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).
Input The first line of the input contains the single number N. Each of next N lines contains one number from the given set.
Output In case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order. If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.
Sample Input 5
1
2
3
4
1
Sample Output 2
2
3
*/
/*
解析来着摘抄
抽屉原理,又叫鸽巢原理
题意:
给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解,
随意输出一组即可。若不存在,输出 0。
题解:
首先必须声明的一点是本题是一定是有解的。原理根据抽屉原理:
因为有n个数,对n个数取余,如果余数中没有出现0,根据鸽巢原理,一定有两个数的余数相同,
如果余数出现0,自然就是n的倍数。也就是说,n个数中一定存在一些数的和是n的倍数。
本题的思路是从第一个数开始一次求得前 i(i <= N)项的和关于N的余数sum,并依次记录相应余数的存在状态,
如果sum == 0;则从第一项到第i项的和即满足题意。如果求得的 sum 在前边已经出现过,假设在第j(j<i)项出现
过相同的 sum 值,则从第 j+1 项到第i项的和一定满足题意。
*/
#include <iostream>
#include<cmath>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxn 16000
int a[maxn];
int sum[maxn];
int main()
{
int i,j,k,n,flag;
while(scanf("%d",&n)!=EOF)
{
flag=false;
sum[]=;
for(i=; i<=n; i++)
{
scanf("%d",&a[i]);
} for(i=; i<=n; i++)
{
sum[i]=(sum[i-]+a[i])%n;
if(sum[i]==)
{ printf("%d\n",i);
for(k=; k<=i; k++)
printf("%d\n",a[k]);
break;
}
else
{
for(j=; j<i; j++)
{
if(sum[i]==sum[j])
{
printf("%d\n",i-j);
for(k=j+; k<=i; k++)
printf("%d\n",a[k]);
flag=true;
break;
}
}
}
if(flag)
break;
}
}
return ;
}
poj2356 Find a multiple的更多相关文章
- [POJ2356]Find a multiple 题解(鸽巢原理)
[POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...
- POJ-2356 Find a multiple(DFS,抽屉原理)
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7133 Accepted: 3122 Speci ...
- [POJ2356] Find a multiple 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8776 Accepted: 3791 ...
- poj2356 Find a multiple(抽屉原理|鸽巢原理)
/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...
- POJ2356 Find a multiple 抽屉原理(鸽巢原理)
题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...
- [poj2356]--Find a multiple ——鸽巢原理
题意: 给定n个数,从中选取m个数,使得\(\sum | n\).本题使用Special Judge. 题解: 既然使用special judge,我们可以直接构造答案. 首先构造在mod N剩余系下 ...
- Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
随机推荐
- Makefile特殊标签
http://www.gnu.org/software/make/manual/html_node/Special-Targets.html
- 【Spring实战】Spring容器初始化完成后执行初始化数据方法
一.背景知识及需求 在做WEB项目时,经常在项目第一次启动时利用WEB容器的监听.Servlet加载初始化等切入点为数据库准备数据,这些初始化数据是系统开始运行前必须的数据,例如权限组.系统选项.默认 ...
- Shell 关闭指定进程
例如要关闭jupyter-notebook这个进程: - | 说明:管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入. “ps -ef” 查看所有进程 “grep -v g ...
- DevOps实践
云计算 [ 01 ]GitLab+Rancher实践DevOps[转载] [ 02 ]Kubernetes CentOS 7.4搭建Kubernetes 1.8.5集群 kubeadm搭建kubern ...
- Java受检异常和不受检异常
Java异常有checked exception(受检异常)和unchecked exception(不受检异常), 编译器在编译时,对于受检异常必须进行try...catch或throws处理,否则 ...
- Eclipse web工程 部署 三种方式 1
Eclipse web工程 部署 三种方式 1.run on 前提: 安装 好 eclipse.jdk.tomcat 然后 新建一个 web工程 注意此处Default output folder 最 ...
- 新的开源java反汇编程序Procyon
wiki:https://bitbucket.org/mstrobel/procyon/wiki/Java%20Decompiler 由于jd好多年没更新了,今天找到这个新的开源反汇编,很不错 分享一 ...
- 重温CLR(十二) 委托
回调函数是一种非常有用的编程机制,它的存在已经有很多年了..NET通过委托来提供回调函数机制.不同于其他平台(比如非托管C++)的回调机制,委托的功能要多得多.例如,委托确保回调方法是类型安全的(这是 ...
- LeetCode 739. Daily Temperatures
原题链接在这里:https://leetcode.com/problems/daily-temperatures/description/ 题目: Given a list of daily temp ...
- 动态添加 items to PopupMenu
引用自http://stackoverflow.com/questions/10175140/adding-items-to-popupmenu DevExpress.XtraBars.BarMana ...