[POJ 2356] Find a multiple
Find a multiple
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 6535 | Accepted: 2849 | 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.
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
Source
Ural Collegiate Programming Contest 1999
鸽巢定理,注意代码实现的细节,0MS ac
#include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; #define N 10010 struct Node { int x,y,s; //下标,余数,前缀和 bool operator <(const Node &t)const { if(y!=t.y) return y<t.y; return x<t.x; } }p[N]; int main() { int n; int flag; int a[N]; while(scanf("%d",&n)!=EOF) { flag=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); p[i].s=p[i-1].s+a[i]; p[i].x=i; p[i].y=p[i].s%n; if(!flag && p[i].y==0) { flag=1; printf("%d\n",i); for(int j=1;j<=i;j++) printf("%d\n",a[j]); } } if(flag) continue; sort(p+1,p+n+1); for(int i=2;i<=n;i++) { if(p[i].y==p[i-1].y) { int &l=p[i-1].x; int &r=p[i].x; printf("%d\n",r-l); for(int j=l+1;j<=r;j++) printf("%d\n",a[j]); break; } } } return 0; }
趁着还有梦想、将AC进行到底~~~by 452181625