首页 > 代码库 > POJ 2356 - Find a multiple

POJ 2356 - Find a multiple

鸽笼原理题,以后得好好研究下相关题目。

 1 /* 2 ID:esxgx1 3 LANG:C++ 4 PROG:poj2356 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm>10 using namespace std;11 12 #define NN    1000713 14 int _sum[NN], *sum = &_sum[1];15 int lookup[NN];16 17 int main(void)18 {19     #ifndef ONLINE_JUDGE20     freopen("in.txt", "r", stdin);21     #endif22 23     int N, i;24     scanf("%d", &N);25     sum[-1] = 0;26     for(i=0; i<N; ++i) {27         int k;28         scanf("%d", &k);29         sum[i] = sum[i-1] + k;30         if (lookup[sum[i] % N]) k = lookup[sum[i]  % N];31         else if (!(sum[i] % N)) k = 0;32         else k = -1;33 34         if (k >= 0) {35             printf("%d\n", i-k+1);36             while(k <= i) {37                 printf("%d\n", sum[k] - sum[k-1]);38                 ++k;39             }40             break;41         } else lookup[sum[i] % N] = i+1;42     } 43     if (i >= N) printf("0\n");44     return 0;45 }

 

2356Accepted744K94MSG++751B2014-07-29 08:35:46