http://poj.org/problem?id=3370
鸽巢原理
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<set> #include<map> #include<string> #include<queue> #include<stack> #include <iomanip> using namespace std; #define LL long long #define ULL unsigned long long const double eps=1e-6; const int INF=0x3f3f3f3f; const int N=100005; int a[N]; int f[N]; int main() { //freopen("data.in","r",stdin); int n,m; while(scanf("%d %d",&n,&m)!=EOF) { if(n==0&&m==0) break; memset(f,-1,sizeof(f)); for(int i=1;i<=m;++i) scanf("%d",&a[i]); int l,r; int k=0; f[k]=0; for(int i=1;i<=n;++i) { k=(k+a[i])%n; if(f[k]==-1) f[k]=i; else {l=f[k];r=i;break;} } for(int i=l+1;i<=r;++i) { printf("%d",i); if(i<r) printf(" "); } printf("\n"); } return 0; }