How many integers can you find
Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7439 Accepted Submission(s): 2200
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
题意:给你m个数 可能含有0 问有多少小于n的正数能整除这个m个数中的某一个
题解:特判除零 容斥原理 这m个数 组合时不能直接相乘 应当取最小公倍数
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
ll gcd(ll aa,ll bb)
{
if(bb==)
return aa;
else
return gcd(bb,aa%bb);
}
ll lcm(ll aa,ll bb)
{
return aa*bb/gcd(aa,bb);
}
ll n,m;
ll que[];
ll a[];
ll ggg;
ll coun;
ll slove(ll gg)
{
ll t=,sum=;
a[t++]=-;
for(ll i=;i<coun;i++)
{ ll k=t;
for(ll j=;j<k;j++)
{
a[t++]=que[i]*a[j]*(-);
if(a[t-]>)
a[t-]=lcm(que[i],-a[j]);
else
a[t-]=-lcm(que[i],a[j]);
}
}
for(ll i=;i<t;i++)
sum=sum+(gg/a[i]);
return sum;
}
int main()
{
while(scanf("%I64d %I64d",&n,&m)!=EOF)
{
memset(que,,sizeof(que));
memset(a,,sizeof(a));
coun=;
for(ll i=;i<m;i++)
{
scanf("%I64d",&ggg);
if(ggg!=)
que[coun++]=ggg;
}
printf("%I64d\n",slove(n-));
}
return ;
}