码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2356 暴力或者组合数学

时间:2014-11-24 22:42:57      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:鸽笼原理

Find a multiple
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6281   Accepted: 2740   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
题意:给你n个数问你存不存在k个数(1<=k<=n)使得其和能整出n;能的话输出其个数及数,不能的话输出0(没有不可能的情况,详情请往下看)
先贴一个暴力的代码;
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <limits.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <functional>
#define N 100010
#define LL long long
#define mem(a) memset(a,0,sizeof(a));
#define mem_1(a) memset(a,-1,sizeof(a));
using namespace std;
LL a[N];
int n;
int i,j;
int work()
{
    for(i=0; i<n; i++)
    {
        int sum=0;
        for(j=i; j<n; j++)
        {
            sum+=a[j];
            if(sum%n==0)
            {
                return 0;
            }
        }
    }
    return 1;
}
int main()
{

    while(cin>>n)
    {
        for(int b=0; b<n; b++)
            cin >> a[b];
        work();
        cout<<j-i+1<<endl;
        for(int b=i; b<=j; b++)
            cout<<a[b]<<endl;
    }
    return 0;
}

之前是因为看鸽笼原理(抽屉原理)才找到的这道题 木想到这么水。。。
现在介绍下鸽笼原理:
    简单定义: “如果有五个鸽子笼,养鸽人养了6只鸽子,那么当鸽子飞回笼中后,至少有一个笼子中装有2只鸽子。”这个简单的事实就是著名的鸽笼原理,在我们国家更多地称为抽屉原理
  对于这道题,我们先设sum[1]=a[1],sum[2]=a[1]+a[2],sum[3]=a[1]+a[2]+a[3],sum[n]=a[1]+a[2]+....a[n];
如果存在sum[i]正好是n的倍数,直接输出就是,但若没有存在,则sum[i]%n必定属于[1,n-1],因为sum[i]有n项,那么鸽笼原理来了,由鸽笼原理知必定有一对sum[i]==sum[j]&&i!=j,而这两个sum的差也就是n的倍数;所以只需要输出i+1到j的值即可,也解释了为啥没有不可能的情况。
下面贴上代码;
#include <queue>
#include <stack>
#include <math.h>
#include <vector>
#include <limits.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <functional>
#define N 100010
#define LL long long
#define mem(a) memset(a,0,sizeof(a));
#define mem_1(a) memset(a,-1,sizeof(a));
using namespace std;
int n;
int f[N];
int sum[N];
int pos[N];
void work()
{
    memset(pos, -1, sizeof(pos));
    pos[0] = 0;
    for (int i = 1; i <= n; i++)
        if (pos[sum[i]] == -1)
            pos[sum[i]] = i;
        else
        {
            printf("%d\n", i - pos[sum[i]]);
            for (int j = pos[sum[i]] + 1; j <= i; j++)
                printf("%d\n", f[j]);
            return;
        }
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &f[i]);
    sum[0] = f[0] = 0;
    for (int i = 1; i <= n; i++)
        sum[i] = (sum[i - 1] + f[i]) % n;
    work();
    return 0;
}



poj 2356 暴力或者组合数学

标签:鸽笼原理

原文地址:http://blog.csdn.net/bigsungod/article/details/41450729

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!