Given n positive integers, select any number from them so that their sum can be divisible by N.
Question: Combined mathematics P17
#include<cstdio>#include<cstring>using namespace std;const int MAXN = 20000;int sum[MAXN], num[MAXN], flag[MAXN];void print ( int s, int t ){ printf("%d\n", t - s + 1); for ( int i = s; i <= t; i++ ) printf("%d\n",num[i]);}int main(){ int n, i; scanf("%d",&n); for ( sum[0] = 0, i = 1; i <= n; i++ ) { scanf("%d",&num[i]); sum[i] = sum[i-1] + num[i]; } memset(flag,0,sizeof(flag)); for ( i = 1; i <= n; i++ ) { int t = sum[i] % n; if ( t == 0 ) { print ( 1, i ); break; } if ( flag[t] ) { print ( flag[t] + 1, i ); break; } flag[t] = i; } return 0;}
Below is 3370
#include<cstdio>#include<cstring>using namespace std;#define lint __int64const int MAXN = 110000;lint sum[MAXN];int num[MAXN], flag[MAXN];void print ( int s, int t ){ for ( int i = s; i < t; i++ ) printf("%d ",i); printf("%d\n",t);}int main(){ int c, n, i; while ( scanf("%d%d",&c,&n) ) { if ( !c && !n ) break; for ( sum[0] = 0, i = 1; i <= n; i++ ) { scanf("%d",&num[i]); sum[i] = sum[i-1] + num[i]; } int start, end, t, f = 0; memset(flag,0,sizeof(flag)); for ( i = 1; i <= n && !f; i++ ) { t = sum[i] % c; if ( t == 0 && sum[i] >= c ) { start = 1, end = i; f = 1; break; } if ( flag[t] && sum[i] - sum[flag[t]] >= c ) { start = flag[t] + 1, end = i, f = 1; break; } flag[t] = i; } if ( f ) print (start, end); else printf("no sweets\n"); } return 0;}