POJ-2356 Find a multiple(DFS,抽屉原理)
Find a multiple
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7133 Accepted: 3122 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.
Sample Input
5
1
2
3
4
1
Sample Output
2
2
3
dfs:
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int a[10005];
int vis[10005];
int n;
int m;
int dfs(int x,int sum)
{
if(sum%n==0&&sum>=n)
{
cout<<m<<endl;
return 1;
}
for(int i=x+1;i<=n;i++)
{
m++;
if(dfs(i,sum+a[i]))
{
cout<<a[i]<<endl;
return 1;
}
m--;
}
return 0;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
m=0;
if(!dfs(0,0))
printf("0\n");
}
return 0;
}
抽屉原理改天补上
POJ-2356 Find a multiple(DFS,抽屉原理)的更多相关文章
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2356 Find a multiple 抽屉原理
从POJ 2356来体会抽屉原理的妙用= =! 题意: 给你一个n,然后给你n个数,让你输出一个数或者多个数,让这些数的和能够组成n: 先输出一个数,代表有多少个数的和,然后再输出这些数: 题解: 首 ...
- POJ- Find a multiple -(抽屉原理)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6452 Accepted: 2809 Special Judge D ...
- poj 2356 Find a multiple(鸽巢原理)
Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...
- poj 2356 Find a multiple【鸽巢原理 模板应用】
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6651 Accepted: 2910 ...
- [POJ 2356] Find a multiple
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6535 Accepted: 2849 ...
- POJ 2356 Find a multiple( 鸽巢定理简单题 )
链接:传送门 题意:题意与3370类似 注意:注意输出就ok,输出的是集合的值不是集合下标 /***************************************************** ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- Find a multiple POJ - 2356 (抽屉原理)
抽屉原理: 形式一:设把n+1个元素划分至n个集合中(A1,A2,…,An),用a1,a2,…,an分别表示这n个集合对应包含的元素个数,则:至少存在某个集合Ai,其包含元素个数值ai大于或等于2. ...
随机推荐
- html实体转换
摘要: 在 HTML 中,某些字符是预留的.在 HTML 中不能使用小于号(<)和大于号(>),这是因为浏览器会误认为它们是标签.如果希望正确地显示预留字符,我们必须在 HTML 源代码中 ...
- 8 -- 深入使用Spring -- 1...2 Bean后处理器的用处
8.1.2 Bean后处理器的用处 Spring提供的两个常用的后处理器: ⊙ BeanNameAutoProxyCreator : 根据Bean实例的name属性,创建Bean实例的代理. ⊙ De ...
- Memcache未授权访问漏洞
Memcached 分布式缓存系统,默认的 11211 端口不需要密码即可访问,黑客直接访问即可获取数据库中所有信息,造成严重的信息泄露. 0X00 Memcache安装 1. 下载Mencache的 ...
- informix中的时间计算
今天看SUN服务器是的mail(vi /var/mail/xxxuser),发现定时任务上的一些存储过程执行有错误,其中有一个错误是long transaction,长事务错误,到数据库一查,天哪 ...
- Nginx 过期时间
客户端(浏览器)访问服务器上的资源后,会缓存在浏览器中,对于一些经常变更的静态资源,我们可以设置缓存时间,也就是设置静态资源的过期时间 [root@localhost ~]$ cat /usr/loc ...
- UML类图的几种关系总结【转】
在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregation),组合(Composit ...
- IOS 通过脚本自动打包工具 webfrogs/xcode_shell
博文转载至 http://www.2cto.com/kf/201506/408346.html ios 开发通过xcode 打包其实效率不是太高,所以就有人,用shell 写了一个,自动打包,发邮件, ...
- Matlab练习——多项式和一元方程求解
1. 一元函数 f(x) = x^3 + (x - 0.98)^2 / (x + 1.25)^3 - 5 * (x + 1 / x),求f(x) = 0 的根 %在函数文件中定义函数 function ...
- open-falcon之transfer
功能 负责数据转发,接受agent上报的数据,然后使用一致性hash规则对数据进行分片,最后将分片后的数据分别转发至judge,graph 对接收到的数据进行合法性校验.规整 针对每个后端实例维护一个 ...
- 日记整理---->2017-05-17
起始时间是2017-05-17,记录一下spring的学习过程.陌生人可以变成熟人,但熟人一旦变成陌生人,就再也回不去了. 一.测试一下init-method和depend-on huhx.xml文件 ...