I heard about the principle of the drawer in the middle school. Unfortunately, I have never had the opportunity to see it. Now I have a conclusion on the principle of the pigeon cage. But I do not know the principle of the pigeon cage. I can summarize the principle of the pigeon cage first: if n + 1 items or n + 1 items are to be placed in n drawers, at least one drawer contains two or more items. If you know this conclusion: a1, a2, a3... am is a positive integer sequence with at least integers k and r, 1 <= k <r <= m, making ak + a (k + 1) +... + a (r) is a multiple of m. The proof is relatively simple: Sk indicates the sum of the number of the first k. (1) If Sk % m = 0, the number of the first k is a multiple of m (2) if Sn and St mode m are the same, the sum of the numbers from t + 1 to n is m equal to 0. even if you don't know this conclusion, if DP is so powerful, you should be able to think of using the first n concepts and the thinking. Conclusion: inference 1: m only pigeons, n cages, at least one pigeon Cage contains no less than m-1) /n] + 1 pigeon. Inference 2: If n m-1) + 1 balls are put into n boxes, at least one box has m balls. Inference 3: If m1, m2 ,... mn is n positive integers, and (m1 + m2 +... + mn)/n> R-1, m1, m2 ,... there must be at least one number of mn codes no less than r: no explanation, more than 700 MS. When judge was at that time, I was afraid of TLE.
#include<cstdio> #include<cstring> using namespace std; #define N 100002 int sum[N],pos[N]; int main() { int c,n,i,r,t,j; while(scanf("%d%d",&c,&n),c+n) { memset(pos,-1,sizeof(pos)); bool flag=false; scanf("%d",&sum[0]); sum[0]%=c; pos[sum[0]]=0; if(sum[0]==0){printf("1\n");flag=1;} for(i=1;i<n;i++) { scanf("%d",&sum[i]); if(flag)continue; sum[i]%=c; sum[i]+=sum[i-1]; sum[i]%=c; if(sum[i]==0) { for(j=0;j<i;j++) printf("%d ",j+1); printf("%d\n",i+1); flag=1; continue; } if(pos[sum[i]]==-1)pos[sum[i]]=i; else { for(j=pos[sum[i]]+1;j<=i;j++) if(j!=i)printf("%d ",j+1); else printf("%d\n",i+1); flag=1; } } } return 0; }