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. ...
 
随机推荐
- 系统安装SQL Sever2000后1433端口未开放,如何打开1433端口的解决方法
			
这篇文章主要针对Win2003系统安装SQL Sever2000后1433端口未开放,如何打开1433端口的解决方法. 用了几年的Windows2003和SQL Server2000了,不过这个问题倒 ...
 - Fade out transition effect using CSS3
			
摘要: css3已经被应用到很多网站了,对于创建动态交互的网站是非常有益的.今天就分享一个使用transition实现的鼠标悬停淡阴淡出的效果. 代码: <!DOCTYPE html> & ...
 - 为什么React事件处理函数必须使用Function.bind()绑定this?
			
最近在React官网学习Handling Events这一章时,有一处不是很明白.代码如下: class Toggle extends React.Component { constructor(pr ...
 - layer的用法
			
layer的用法: 使用之前必须导入包如下: #import<QuartzCore/QuartzCore.h> 否则使用控件.layer时,是不会提示的,粘贴过来的代码会直接报错. 就拿v ...
 - Missing iOS Distribution signing identity
			
打包上传appstore的时候报错如下: 解决方法: Download https://developer.apple.com/certificationauthority/AppleWWDRCA.c ...
 - java  递归
			
package com.j1.soa.resource.cms.service.oracle; import com.j1.base.dto.ServiceMessage; import com.j1 ...
 - PyQt4显示提示信息
			
我们可以为任何窗口部件设置一个气球提示. #!/usr/bin/python # -*- coding:utf-8 -*- import sys from PyQt4 import QtGui fro ...
 - jquery on=>keyup无法绑定未来js添加的元素
			
$("[name=red_count]").on("keyup", countKeyUpBind);即使使用on的,也无法绑定未来元素, 所以直接在动态添加的时 ...
 - Matlab练习——素数查找
			
输入数字,0结束,判断输入的数字中的素数 clc; %清空命令行窗口的数据 clear; %清除工作空间的变量 k = ; n = ; %素数的个数 zzs(k) = input('请输入正整数: ' ...
 - WP8.1学习系列(第二十三章)——到控件的数据绑定
			
在本文中 先决条件 将控件绑定到单个项目 将控件绑定到对象的集合 通过使用数据模板显示控件中的项目 添加详细信息视图 转换数据以在控件中显示 相关主题 本主题介绍了如何在使用 C++.C# 或 Vis ...