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
Sample Output
题意:给你一个数n,一个含m个元素的集合,求小于n的数中是m中某个元素的倍数有多少个。
深搜的两种实现:
2.用vector实现
12 2 2 3
7
题意:给你一个数n,一个含m个元素的集合,求小于n的数中是m中某个元素的倍数有多少个。
题目分析:这是一道简单的容斥原理的题目,下面根据这道题给出容斥原理的三种实现方法: dfs,队列数组,二进制;
注意:这道题求的是小于n的数中有多少符合条件的,不包含n本身;
二进制实现:
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
vector<int>x;
int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int solve(int n)
{
int ans=0;
for(int i=1;i<(1<<x.size());i++)
{
int lcm=1,cnt=0;
for(int j=0;j<x.size();j++)
if(i&(1<<j))
{
cnt++;
int g=gcd(lcm,x[j]);
lcm=lcm/g*x[j];
}
if(cnt&1)
ans+=n/lcm;
else
ans-=n/lcm;
}
return ans;
}
int main()
{
int n,m;
int temp;
while(scanf("%d%d",&n,&m)!=EOF)
{
n--;
x.clear();
for(int i=1;i<=m;i++)
{
scanf("%d",&temp);
if(temp) ///不能为0
x.push_back(temp);
}
cout<<solve(n)<<endl;
}
return 0;
}
深搜的两种实现:
1.用数组存储元素
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
int get_lcm(int a,int b)
{
int x=a;
int y=b;
while(b)
{
int t=a;
a=b;
b=t%b;
}
return x/a*y;
}
long long ans;
int b[20],k,n;
void dfs(int i,int cnt,int lcm)///i代表数组的下标
{ ///cnt代表几个元素的最小公倍数
lcm=get_lcm(lcm,b[i]); ///lcm代表最小公倍数
if(cnt&1)
ans+=n/lcm;
else
ans-=n/lcm;
for(int j=i+1;j<k;j++)
dfs(j,cnt+1,lcm);
}
int main()
{
int m;
while(scanf("%d%d",&n,&m)!=EOF)
{
n--;
ans=k=0;
for(int i=1;i<=m;i++)///k记录非0的个数
{
scanf("%d",b+k);///给这个数组赋的值不能为0;
if(!b[k++]) k--;///这种赋值方法有趣
}
for(int i=0;i<k;i++)
dfs(i,1,1);
cout<<ans<<endl;
}
return 0;
}
2.用vector实现
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
int get_lcm(int a,int b)///获得最小公倍数
{
int x=a;
int y=b;
while(b)
{
int t=a;
a=b;
b=t%b;
}
return x/a*y;
}
vector<int>x;
int ans;
int n;
void dfs(int i,int cnt,int lcm)///i代表下标
{ ///cnt代表几个数
lcm=get_lcm(x[i],lcm); ///lcm代表最小公倍数
if(cnt&1)
ans+=n/lcm;
else
ans-=n/lcm;
for(int j=i+1;j<x.size();j++)
dfs(j,cnt+1,lcm);
}
int main()
{
int m;
int temp;
while(scanf("%d%d",&n,&m)!=EOF)
{
n--;
x.clear();
ans=0;
for(int i=1;i<=m;i++)
{
scanf("%d",&temp);
if(temp)
x.push_back(temp);
}
for(int i=0;i<x.size();i++)
dfs(i,1,1);
cout<<ans<<endl;
}
return 0;
}