Reference: http://www.cppblog.com/pcfeng502/archive/2009/10/18/98902.aspx
This question marks "Drawer principle" in the Training Manual. To be honest, I still don't know why it is a drawer principle until I see the specific solution to this question, this clearly judges the remainder of the number, and later finds out the clever concept of the pigeon cage.
45
1 2 3 7 5
For example, for the first group of data,
1 2 3 7 5 Mode4They are:
1 2 3 3 1
If the same remainder + dp method is used, the complexity and rules are relatively complicated;
In this case, we use a clever method, that is, using a sum [I] array to record the sum of the previous I count %4In the example above, we get:
I |
0 (this column is hidden) |
1 |
2 |
3 |
4 |
5 |
Sweet [I] |
0 |
1 |
2 |
3 |
7 |
5 |
Sum [I] |
0 |
1 |
3 |
2 |
1 |
2 |
Therefore, when we encounter a problem where the value of 1.sum[ I] = 0 or 2.sum[ I] already exists, we can understand the solution, in addition, the solution starts from the last position where the sum [I] value appears → the position where sum [I] is now. This seems to be a possible solution; however, due to the random order of addition, it is not clear whether the solution can be solved in the case of a solution. At this time, we will use the pigeon cage principle.
Using the method in the discrete mathematics and composite mathematics book, we can set sum [1 ~ N = nneighbor] (or 0 ~ N) as n + 1 pigeon (note the hidden 0 column above), fly into the c = nchild pigeon cage, and then notice that the subject has such a data range:
The first line of each test case contains two integersCAndN(1 ≤C≤N ≤100000) (This is the most important prerequisite for using pigeon cages in this question)
From this we can know that there must be two pigeons flying into the same cage, so the order of addition does not need to be considered; and we can also know from this, this question cannot be solved.
#include<iostream>#include <stdlib.h>#include <algorithm>#include <stdio.h>#include<cstring>using namespace std;const int N=100009;int n,m;int a[N];int sum[N];int hash[N];int main(){while(scanf("%d%d",&n,&m),n+m){memset(hash,0,sizeof(hash));for(int i=1;i<=m;i++){scanf("%d",&a[i]);}int s=1,t=1;sum[0]=0;for(int i=1;i<=m;i++){sum[i]=(sum[i-1]+a[i])%n;//cout<<i<<" "<<sum[i]<<endl;if(sum[i]==0){t=i;break;}if(hash[sum[i]]>0){s=hash[sum[i]]+1;t=i;break;}hash[sum[i]]=i;}for(int i=s;i<t;i++)printf("%d ",i);printf("%d\n",t);}return 0;}