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. ...
随机推荐
- [RN] 01 - Init: Try a little bit of React Native
Ref: React Native跨平台移动应用开发 后记:这本书博客味有点浓. 本篇涉及新建工程的若干套路,以及一点语法知识. 创建新工程 (1) 解决的一大核心问题: (2) 使用Javascri ...
- Java使用选择排序法对数组排序
编写程序,实现将输入的字符串转换为一维数组,并使用选择排序法对数组进行排序. 思路如下: 点击"生成随机数"按钮,创建Random随机数对象: 使用JTextArea的setTex ...
- C++实现按1的个数排序
题目内容:有一些0.1字符串,将其按1的个数的多少的顺序进行输出. 输入描述:本题只有一组测试数据.输入数据由若干数字组成,它是由若干个0和1组成的数字. 输出描述:对所有输入的数据,按1的个数进行生 ...
- Java -- IO -- 目录
操作文件的类 -- -- File File类的基本介绍 使用File类操作文件 范例 -- -- 列出指定目录的全部内容 RandomAccessFile类 使用RandomAccessFile类写 ...
- 在SSH框架中,如何得到POST请求的URL和参数列表
在做项目的API通知接口的时候,发现在SSH框架中无法获取到对方服务器发来的异步通知信息.最后排查到的原因可能是struts2对HttpServletRequest进行了二次处理,那么该如何拿到pos ...
- webstrom 2017 安装及配置
下载安装:http://www.jetbrains.com/webstorm/ 激活:安装完成后,在打开的 License Activation 窗口中选择 License server. 在输入框输 ...
- 【RF库XML测试】parse xml
Name:Parse XmlSource:XML <test library>Arguments:[ source | keep_clark_notation=False ]Parses ...
- 【RF库Collections测试】Set List Value
Name:Set List ValueSource:Collections <test library>Arguments:[ list_ | index | value ]Sets th ...
- [NodeJS] Node.js 编码转换
Node.js 自带的 toString() 方法不支持 gbk,因此中文转换的时候需要加载第三方库,推荐以下两个编码转换库,iconv-lite 和 encoding. iconv, iconv-l ...
- gitlab数据迁移
由于gitlab是默认安装的,随着公司代码越来越多,导致gitlab数据目录空间不足,出现无法访问gitlab了. 磁盘空间: /home有1.8T的空间一直没用上! 现在打算将原有代码目录迁移到新目 ...