Title Link: http://poj.org/problem?id=3370
Test instructions: Each group gives you a number C and a number of M, enter the number of M, let you output a number of subscript, these numbers together can be divided into C (c less than equals N), pigeon nest principle is also applicable,
Note that sum may overflow int, and read in with scanf to prevent timeouts.
Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <iostream> #include <string> #include <queue> #include <stack> #include < vector> #include <algorithm> #define N 100000+100using namespace std;int a[n];long long int sum[n];int yu[n]; struct node{bool k; int POS;} Q[n];int Main () {int N, C; int I, J; int left, right; while (~SCANF ("%d%d", &c, &n)) {if (c==0 && n==0) break; BOOL Flag1=false; BOOL Flag2=false; for (i=0; i<n; i++) {scanf ("%d", &a[i]); if (i==0) sum[i]=a[i]; else Sum[i]=sum[i-1]+a[i]; } left=0; memset (q, 0, sizeof (q)); for (i=0; i<n; i++) {yu[i]=sum[i]%c; if (yu[i]==0) {flag1=true; right=i; break; } else{if (Q[YU[I]].K) {flag2=true; Left=q[yu[i]].pos; right=i; Break }else{q[yu[i]].k=true; q[yu[i]].pos=i; }}} if (Flag1) {//printf ("%d\n", right+1); for (i=0; i<=right; i++) {if (i==0) printf ("%d", i+1); else printf ("%d", i+1); } printf ("\ n"); } else if (Flag2) {//printf ("%d\n", right-left); for (i=left+1; i<=right; i++) {if (i==left+1) printf ("%d", i+1); else printf ("%d", i+1); } printf ("\ n"); }} return 0;}
This is a person on the net to write code, relatively short: http://www.cnblogs.com/ACShiryu/archive/2011/08/09/poj3370.html for reference to learn.
POJ 3370 Halloween Treats