poj 3370 鸽巢
来源:互联网 发布:高楼 网络拓扑结构图 编辑:程序博客网 时间:2024/06/04 01:20
Description
Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a child will get nothing if it is too late. To avoid conflicts, the children have decided they will put all sweets together and then divide them evenly among themselves. From last year's experience of Halloween they know how many sweets they get from each neighbour. Since they care more about justice than about the number of sweets they get, they want to select a subset of the neighbours to visit, so that in sharing every child receives the same number of sweets. They will not be satisfied if they have any sweets left which cannot be divided.
Your job is to help the children and present a solution.
Input
The input contains several test cases.
The first line of each test case contains two integers c and n (1 ≤ c ≤ n ≤ 100000), the number of children and the number of neighbours, respectively. The next line contains n space separated integers a1 , ... , an (1 ≤ ai ≤ 100000 ), where ai represents the number of sweets the children get if they visit neighbour i.
The last test case is followed by two zeros.
Output
For each test case output one line with the indices of the neighbours the children should select (here, index i corresponds to neighbour i who gives a total number of ai sweets). If there is no solution where each child gets at least one sweet print "no sweets" instead. Note that if there are several solutions where each child gets at least one sweet, you may print any of them.
Sample Input
4 51 2 3 7 53 67 11 2 5 13 170 0
Sample Output
3 52 3 4
先给出一个数N,接着再给出N个数,要你从这N个数中任意选择1个或多个数,使得其和是M的倍数(必定有解)
初始化:LL sum[i] 1~i之和,int mod[100010];int a[100010];
#include<iostream>#include<cstring>#include<cmath>#include<cstdio>#include<queue>#include<map>#include<vector>#include<cstdlib>#include<algorithm>#define inf 0x3f3f3f3f#define LL long longusing namespace std;#include<iostream>#include<cstring>#include<cmath>#include<cstdio>#include<queue>#include<map>#include<vector>#include<cstdlib>#include<algorithm>#define inf 0x3f3f3f3f#define LL long longusing namespace std;LL sum[100010];int mod[100010];int a[100010];int main(){ int n,m; while(cin>>m>>n) { if(n==0||m==0) return 0; for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(sum,0,sizeof(sum)); memset(mod,-1,sizeof(mod)); for(int i=1;i<=n;i++) { sum[i]=sum[i-1]+a[i]; if(sum[i]%m==0) { for(int j=1;j<=i-1;j++) cout<<j<<" "; cout<<i<<endl; break; } if(mod[sum[i]%m]!=-1) { for(int j=mod[sum[i]%m]+1;j<=i-1;j++) cout<<j<<" "; cout<<i<<endl; break; } mod[sum[i]%m]=i; } } return 0;}int mod[100010];int a[100010];int main(){ int n,m; while(cin>>m>>n) { if(n==0||m==0) return 0; for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(sum,0,sizeof(sum)); memset(mod,-1,sizeof(mod)); for(int i=1;i<=n;i++) { sum[i]=sum[i-1]+a[i]; if(sum[i]%m==0) { for(int j=1;j<=i-1;j++) cout<<j<<" "; cout<<i<<endl; break; } if(mod[sum[i]%m]!=-1) { for(int j=mod[sum[i]%m]+1;j<=i-1;j++) cout<<j<<" "; cout<<i<<endl; break; } mod[sum[i]%m]=i; } } return 0;}
- poj 3370 鸽巢
- poj 2365 poj 3370
- POJ 3370
- poj 3370
- POJ 3370 Halloween treats
- poj 3370 Halloween treats
- poj 3370 Halloween treats
- POJ 3370 Halloween Treats
- poj 3370 抽屉原理
- poj 3370 Halloween treats
- poj 3370 Halloween treats
- poj 3370 抽屉原理
- poj 3370 Halloween treats
- Poj 3370 Halloween Treats
- poj 3370 抽屉原理
- poj 3370(抽屉原理)
- POJ-3370-Halloween treats
- POJ
- SOAPUI笔记2:SoapUI widow破解
- 高效Android开发者必须知道的4个工具
- 异常
- 从头认识多线程-1.14 优先级
- Python装饰器
- poj 3370 鸽巢
- iOS 集成极光推送(一)
- 第三方库的导入及问题处理
- Hadoop-MapReduce
- 浅析 Android 动画:自定义 Interpolator 与 TypeEvaluator
- openGL下的”橡皮筋“技术(多边形绘制)
- 数据挖掘--Apriori
- 哈夫曼树与哈夫曼编码
- hdu4278 Faulty Odometer 八进制转十进制