HDU 1796 How many integers can you find

来源:互联网 发布:unity3d入门教程 pdf 编辑:程序博客网 时间:2024/05/21 22:29

How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8427    Accepted Submission(s): 2508


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 22 3
 

Sample Output
7
 

Author
wangye
 

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

Recommend
wangye

题意:给两个数m和n,再给n个数,求m中有多少个小于m的数能整除这n个数中的任意一个
很容易想到要用容斥原理,先计算出n个数中每个数的因数有多少,这之中将是两个数最小公倍数的因数多加了一次,再减去n中两个数最小公倍数的因数有多少,又将n中3个数的最小公倍数多减了一次,有要加上这一部分,直到是n中所有数的最小公倍数,根据奇偶性判断是加还是减,不知道计算边界,所以要用到dfs

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<queue>#include<deque>#include<set>#include<map>#include<cmath>#include<vector>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> PII;#define pi acos(-1.0)#define eps 1e-10#define pf printf#define sf scanf#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r#define e tree[rt]#define _s second#define _f first#define all(x) (x).begin,(x).end#define mem(i,a) memset(i,a,sizeof i)#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)#define mi ((l+r)>>1)#define sqr(x) ((x)*(x))const int inf=0x3f3f3f3f;int m,n,num,a[110],ans,d;ll gcd(ll a,ll b)//计算最小公倍数{    return !b?a:gcd(b,a%b);}void dfs(int x,ll y,int z){    y=a[x]/gcd(a[x],y)*y;//先除再乘,防止爆数据    if(z&1)ans+=(m-1)/y;    else ans-=(m-1)/y;    for(int i=x+1;i<num;i++)        dfs(i,y,z+1);}int main(){    while(~sf("%d%d",&m,&n))    {        ans=0;        num=0;        for1(i,n)        {            sf("%d",&d);            if(d)//题目说是非负数,可能为0                a[num++]=d;        }        for0(i,num)            dfs(i,a[i],1);//每次dfs的实际情况是将含这个数的所有公倍数数因数全部解决,到下一次不会用前面的        pf("%d\n",ans);    }    return 0;}


原创粉丝点击