您的位置:首页 > 其它

hdu 1796 How many integers can you find(容斥定理)

2013-08-16 19:38 381 查看


How many integers can you find

Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3132 Accepted Submission(s): 884



Problem Description

Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10},
all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.

Input

There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.

Output

For each case, output the number.

Sample Input

12 2
2 3


Sample Output

7


Author

wangye

Source

2008
“Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(4)

Recommend

wangye

题意:求小于n能被集合m任意一个数整数的数的个数

题解:裸容斥定理...本人第一次写,挑了个简单的....写出来的代码还是挺短的,注意m的集合范围是非负数

#include<stdio.h>
long long n,res;
int a[23],m;
int gcd(int a,int b){ return b==0?a:gcd(b,a%b); }
void dfs(int now,int cou,int lcm)
{
int i;

lcm=a[now]/gcd(lcm,a[now])*lcm;
if(cou&1) res-=(n-1)/lcm;
else res+=(n-1)/lcm;
for(i=now+1;i<m;i++) dfs(i,cou+1,lcm);
}
int main()
{
int i;

while(scanf("%I64d%d",&n,&m)>0)
{
for(res=i=0;i<m;i++)
{
scanf("%d",a+i);
if(!a[i]) i--,m--;
}
for(i=0;i<m;i++) dfs(i,0,1);
printf("%I64d\n",res);
}

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