Find a multiple
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 Source |
和上一道题类似。
由抽屉原理可知,一定不存在无解的情况。
然后这道题。。。wa了多次。。
原因是读错题。。
输出的是原始的a[i],不是i。。。
样例恰好a[2]=2,a[3] =3
真是sad....
1 /************************************************************************* 2 > File Name: code/poj/2356.cpp 3 > Author: 111qqz 4 > Email: [email protected] 5 > Created Time: 2015年08月21日 星期五 13时43分41秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #define y0 abc111qqz 21 #define y1 hust111qqz 22 #define yn hez111qqz 23 #define j1 cute111qqz 24 #define tm crazy111qqz 25 #define lr dying111qqz 26 using namespace std; 27 #define REP(i, n) for (int i=0;i<int(n);++i) 28 typedef long long LL; 29 typedef unsigned long long ULL; 30 const int inf = 0x3f3f3f3f; 31 const int N=2E4+7; 32 int a[N]; 33 int sum[N]; 34 int n; 35 int p[N]; 36 int main() 37 { 38 scanf("%d",&n); 39 sum[0]= 0; 40 for ( int i = 1 ; i <= n ; i++){ 41 scanf("%d",&a[i]); 42 a[i] = a[i]; 43 sum[i] = (sum[i-1] + a[i])%n; 44 } 45 memset(p,0,sizeof(p)); 46 for ( int i = 1 ; i <= n ; i++){ 47 if (sum[i]==0){ 48 printf("%d\n",i); 49 for ( int j = 1 ; j <= i ; j++){ 50 printf("%d\n",a[j]); 51 } 52 break; 53 } 54 if (p[sum[i]]){ 55 // cout<<"111qqz"<<endl; 56 printf("%d\n",i-p[sum[i]]); 57 for ( int j = p[sum[i]]+1 ; j <= i ; j++){ 58 printf("%d\n",a[j]); 59 } 60 break; 61 } 62 p[sum[i]] = i; 63 64 } 65 66 return 0; 67 }