HDU 1796 How many integers can you find

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

Sample Output
  
  
7
 

Author
wangye
 

Source
 

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值