原理简单..N+1个物品放入N个盒子中..必定有一个盒子里物品大于一个...
但要知道什么时候用...1、may print any of them ..Special judge...就像这两题..如果要输出数最少的..估计DP了.
2、有类似取模的运算...
Program ( POJ 2356 )
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> #include<queue> #include<stack> #include<set> #include<map> #include<algorithm> #define ll long long #define eps 1e-5 #define oo 10007 #define pi acos(-1.0) #define MAXN 10005 using namespace std; int sum[MAXN],a[MAXN],times[MAXN]; int main() { int i,j,x,n; sum[0]=0; memset(times,0,sizeof(times)); scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%d",&a[i]); sum[i]=(sum[i-1]+a[i])%n; times[sum[i]]++; } if (times[0]) { for (i=1;i<=n;i++) if (sum[i]==0) break; printf("%d\n",i); for (x=1;x<=i;x++) printf("%d\n",a[x]); }else { for (x=1;x<n;x++) if (times[x]>=2) break; for (i=1;i<=n;i++) if (sum[i]==x) break; for (j=i+1;j<=n;j++) if (sum[j]==x) break; printf("%d\n",j-i); for (x=i+1;x<=j;x++) printf("%d\n",a[x]); } return 0; }
Program ( POJ 3370 )
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> #include<queue> #include<stack> #include<set> #include<map> #include<algorithm> #define ll long long #define eps 1e-5 #define oo 10007 #define pi acos(-1.0) #define MAXN 100005 using namespace std; int sum[MAXN],a[MAXN],times[MAXN]; int main() { int i,j,x,c,n; while (~scanf("%d%d",&c,&n) && n && c) { sum[0]=0; memset(times,0,sizeof(times)); for (i=1;i<=n;i++) { scanf("%d",&a[i]); sum[i]=(sum[i-1]+a[i])%c; times[sum[i]]++; } if (times[0]) { for (i=1;i<=n;i++) if (sum[i]==0) break; for (x=1;x<i;x++) printf("%d ",x); printf("%d",i); }else { for (x=1;x<c;x++) if (times[x]>=2) break; for (i=1;i<=n;i++) if (sum[i]==x) break; for (j=i+1;j<=n;j++) if (sum[j]==x) break; for (x=i+1;x<j;x++) printf("%d ",x); printf("%d",j); } printf("\n"); } return 0; }