您的位置:首页 > 其它

poj 2356 暴力或者组合数学

2014-11-24 19:59 281 查看
Find a multiple
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 6281Accepted: 2740Special Judge
DescriptionThe 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 ofgiven 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).InputThe first line of the input contains the single number N. Each of next N lines contains one number from the given set.OutputIn 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 separateline 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
;
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;
}
之前是因为看鸽笼原理(抽屉原理)才找到的这道题 木想到这么水。。。[/code]
现在介绍下鸽笼原理:
简单定义: “如果有五个鸽子笼,养鸽人养了6只鸽子,那么当鸽子飞回笼中后,至少有一个笼子中装有2只鸽子。”这个简单的事实就是著名的鸽笼原理,在我们国家更多地称为抽屉原理
对于这道题,我们先设sum[1]=a[1],sum[2]=a[1]+a[2],sum[3]=a[1]+a[2]+a[3],sum
=a[1]+a[2]+....a
;
如果存在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
;
int sum
;
int pos
;
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;
}
[/code]

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: