码迷,mamicode.com
首页 > 其他好文 > 详细

Find a multiple POJ - 2356

时间:2020-02-21 20:07:51      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:equal   space   tip   https   include   ems   没有   ons   这一   

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

 

 

这道题用到了鸠巢原理,鸠巢原理参考链接:https://blog.csdn.net/guoyangfan_/article/details/102559097
下面我借用几个上面博客的鸠巢原理内容
 
定理 如果把n+1个物体放入n个盒子,至少有一个盒子包含两个或更多的物体。
证明 反证法 n个盒子每个盒子至多一个物品,总数至多为n,与有n+1个物体矛盾。
例 1.1
求证:整数a1,a2,a3,…,am(ai不是m的倍数),至少有两个数ai,aj除以m后余数相同。
证明:显然余数分布在1到m-1之间,共m-1种情况,而整数有m个,所以至少有两个数ai,aj除以m后余数相同。
变式 1.1.1
?求证:有理数中的无限位小数在小数点后某一位必开始循环
?证明:由有理数定义我们可设该有理数为N/M(N,M∈Z且M!=0),那么根据竖式除法的原则,求值过程中不断更新的是分子N的值,由于不同分子都是由上次的分子对分母取模所得知,取M个不同的分子,那么根据鸽巢原理,他们中至少有两个数关于M同余,那么下一位结果也就循环了。得证。
技术图片

 

 

 
题意:
这道题给你了n个数,让你找这n个数中有没有几个数的和是n的倍数
 
题解:
你循环遍历一遍这n个数,如果某个数是n的倍数,那就输出一个1再输出这个数
如果没有的话,那就对这n个数求一下求前缀和。
1、在循环遍历一遍这个前缀和,如果某个数是n的倍数,那就输出i,再循环打印出1到i的值(这个i是我们假设的一个下标)
2、如果没有n的倍数的话,那就肯定至少有两个数取余于n的结果一样
是不是想问为什么?嘿嘿
你想一共有n个数,而且这里面没有n的倍数,那么都取余于n之后是没有0的
但是1——n这才一共n-1个数不相同,而你有n个数,那么我们上面的话就得以证明了^_^
 
我们接着说,有了两个数取余于n结果一样,比如是1——i的前缀和 和 1——j的前缀和 取余于n的结果一样
那么i+1——j这一段所有数的和不久正是n的倍数嘛。

 

代码:

 1 /*
 2 这道题用到了鸠巢原理,我的参考链接:https://blog.csdn.net/guoyangfan_/article/details/102559097
 3 
 4 题意:
 5 这道题给你了n个数,让你找这n个数中有没有几个数的和是n的倍数
 6 
 7 题解:
 8 你循环遍历一遍这n个数,如果某个数是n的倍数,那就输出一个1再输出这个数
 9 如果没有的话,那就对这n个数求一下求前缀和。
10 1、在循环遍历一遍这个前缀和,如果某个数是n的倍数,那就输出i,再循环打印出1到i的值(这个i是我们假设的一个下标)
11 2、如果没有n的倍数的话,那就肯定至少有两个数取余于n的结果一样
12 是不是想问为什么?嘿嘿
13 你想一共有n个数,而且这里面没有n的倍数,那么都取余于n之后是没有0的
14 但是1——n这才一共n-1个数不相同,而你有n个数,那么我们上面的话就得以证明了^_^
15 
16 我们接着说,有了两个数取余于n结果一样,比如是1——i的前缀和 和 1——j的前缀和 取余于n的结果一样
17 那么i+1——j这一段所有数的和不久正是n的倍数嘛。
18 */
19 #include<stdio.h>
20 #include<string.h>
21 #include<iostream>
22 #include<algorithm>
23 using namespace std;
24 typedef long long ll;
25 const int maxn=10005;
26 int v[maxn],sum[maxn],p[15005];
27 int main()
28 {
29     
30     int n,flag=0;
31     scanf("%d",&n);
32     for(int i=1;i<=n;++i)
33     {
34         scanf("%d",&v[i]);
35         if(v[i]%n==0)
36         {
37             flag=i;
38         }
39         sum[i]=(v[i]+sum[i-1])%n;
40     }
41     if(flag)
42     {
43         printf("1\n%d\n",v[flag]);
44     }
45     else
46     {
47         for(int i=1;i<=n;++i)
48         {
49             if(p[sum[i]])
50             {
51                 printf("%d\n",i-p[sum[i]]);
52                 for(int j=p[sum[i]]+1;j<=i;++j)
53                     printf("%d\n",v[j]);
54                 flag=0;
55                 break;
56             }
57             else p[sum[i]]=i;
58             if(sum[i]==0)
59             {
60                 flag=i;
61                 break;
62             }
63         }
64         if(flag)
65         {
66             printf("%d\n",flag);
67             for(int i=1;i<=flag;++i)
68                 printf("%d\n",v[i]);
69         }
70     }
71     return 0;
72 }

 

Find a multiple POJ - 2356

标签:equal   space   tip   https   include   ems   没有   ons   这一   

原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/12342559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!