/*
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的更多相关文章

  1. [POJ2356]Find a multiple 题解(鸽巢原理)

    [POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...

  2. POJ-2356 Find a multiple(DFS,抽屉原理)

    Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7133 Accepted: 3122 Speci ...

  3. [POJ2356] Find a multiple 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8776   Accepted: 3791   ...

  4. poj2356 Find a multiple(抽屉原理|鸽巢原理)

    /* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...

  5. POJ2356 Find a multiple 抽屉原理(鸽巢原理)

    题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...

  6. [poj2356]--Find a multiple ——鸽巢原理

    题意: 给定n个数,从中选取m个数,使得\(\sum | n\).本题使用Special Judge. 题解: 既然使用special judge,我们可以直接构造答案. 首先构造在mod N剩余系下 ...

  7. 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 ... 这个错误是因为有两个相 ...

  8. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  9. [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 ...

随机推荐

  1. PHP中cookies跨目录无法调用

    <?php  setcookie("messagetime",time(),time()+60);  ?> 写法无法跨目录调用呢.在搜索引擎中搜索,并没有找到答案(可能 ...

  2. 使用gmock白盒测试

    提起白盒测试,很多程序员可能觉得就是个书上的概念,很多人写完代码根本没有具体的测试方案,自己觉得可行就提交了,其实这是个很危险的事情,毕竟出了bug,最后要加班的人还是你 ,因此做好白盒测试,100% ...

  3. Office for Mac

    前情 真的用不惯Mac上的pages,所以就找Mac版的office.找了一个发觉不对,安装总是出错,当时也没有太大的需求,就把这事搁置了下来.今天要写技术文档的时候才终于下决心要把office下再下 ...

  4. ios 控制器的生命周期

    #pragma mark - 控制器生命周期 // 视图将要出现 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:anima ...

  5. 快速排序的partition版本实现

    int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low-1; for (int j = low ...

  6. BZOJ4832: [Lydsy1704月赛]抵制克苏恩(记忆化&期望)

    Description 小Q同学现在沉迷炉石传说不能自拔.他发现一张名为克苏恩的牌很不公平.如果你不玩炉石传说,不必担心,小Q 同学会告诉你所有相关的细节.炉石传说是这样的一个游戏,每个玩家拥有一个 ...

  7. 每天一个linux命令(文件操作):【转载】locate命令

    locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...

  8. DependencyProperty.UnsetValue 的正确打开方式

    无论是 WPF,还是 UWP,只要你用了绑定或者标记扩展,一定会碰到一个神奇的值——DependencyProperty.UnsetValue.UnsetValue 是什么意思?为什么会出现这个值呢? ...

  9. [Luogu4715]「英语」Z 语言

    luogu description 你有一个长度为\(n\)的串\(A\)和一个长度为\(m\)的串\(B\),字符集大小\(2^{31}\),且同一个串中没有相同的元素. 定义\(B\)串与\(A_ ...

  10. smarty核心思想 自制模板引擎

    <?php $tit = '今天下雨了,淋了半条街'; function tit($file){ //读文件 $h = file_get_contents($file); $h = str_re ...