poj 2356 poj3370
来源:互联网 发布:平板电脑推荐 知乎 编辑:程序博客网 时间:2024/05/17 05:13
Find a multiple
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7705 Accepted: 3362 Special Judge
Description
The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).
Input
The first line of the input contains the single number N. Each of next N lines contains one number from the given set.
Output
In case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order.
If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.
If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.
Sample Input
512341
Sample Output
223
鸽巢原理入门题目:
题意:
求出n个数字里面,是否可以由几个数字组合成n的倍数,有就输出个数和这些数字分别是什么,没有就只输出0
题解:
由鸽巢原理可以知道,总会存在解,而且这个解我们可以看做是连续的数字段组合成的
sum【i】=sum【i-1】+a【i】
如果sum数组中有满足sum【i】%n==0的元素,那么就是答案
否则,sum数组中有n个元素,但是sum【i】%n最多得到n-1个不同的数字
因此就一定存在模n得到相同结果的sum元素,这两个元素之间就是答案
因为(sum【i】-sum【j】)%n==0;
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n;int a[10005];int visit[10005];int main(){ //freopen("in.txt","r",stdin); while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(visit,0,sizeof(visit)); int sum=0; for(int i=1;i<=n;i++){ sum=(sum+a[i])%n; if(sum==0){ printf("%d\n",i); for(int j=1;j<=i;j++) printf("%d\n",a[j]); break; } else if(visit[sum]){ printf("%d\n",i-visit[sum]); for(int j=visit[sum]+1;j<=i;j++) printf("%d\n",a[j]); break; } else visit[sum]=i; } } return 0;}
poj3370
题意:
给定m个数字,求其中某个子集是n的倍数
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,m;int a[100005];int visit[100005];int main(){ //freopen("in.txt","r",stdin); while(scanf("%d%d",&n,&m)&&(n||m)) { for(int i=1;i<=m;i++) scanf("%d",&a[i]); memset(visit,0,sizeof(visit)); int sum=0; for(int i=1;i<=m;i++){ sum=(sum+a[i])%n; if(sum==0){ for(int j=1;j<=i;j++) printf(i!=j?"%d ":"%d\n",j); break; } else if(visit[sum]){ for(int j=visit[sum]+1;j<=i;j++) printf(i!=j?"%d ":"%d\n",j); break; } else visit[sum]=i; } } return 0;}
0 0
- poj 2356 poj3370
- poj3370
- poj3370(鸽巢原理-与2356一样)
- poj3370 抽屉原理
- poj3370,抽屉原理
- poj3370 鸽笼原理
- HDU1808&&POJ3370-Halloween treats
- poj3370 抽屉原理
- poj3370——Halloween treats
- POJ3370 Halloween treats 抽屉原理
- 抽屉原理-poj3370-Halloween treats
- poj3370 poj2356 鸽巢定理
- POJ3370&HDU1808 Halloween treats【鸽巢原理】
- POJ3370 Halloween treats(数论:鸽巢原理)
- 【HDU1205/POJ2356/POJ3370】鸽巢原理专题
- 鸽巢原理 Poj3370&Hdu1808 + Poj2356 + Hdu 1205
- poj 2356
- POJ 2356 抽屉原理
- spring aop(面向切面) 详解
- vec
- HashCode 和 Equals 的使用 - 使用自定义对象作为HashMap的Key例子
- 项目管理之甘特图
- 這是我的java程序之路的開始
- poj 2356 poj3370
- [LeetCode]1. Two Sum
- 代码片段 notepad++
- 代码片段notepad++ 2
- 英文原文:MVC vs. MVP vs. MVVM
- php访问数据库出错:Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given i
- LeetCode 6: ZigZag Conversion
- PHP+MySQL分页显示示例分析
- 怎样卸载干净Oracle