Interesting Problem
Description
Dearway knows some people like math problems but other not. Now the fact is in front of you all.
Give n integers x1 x2 ... xn, you should count how many integers from 1 to m that can be divided by at least one of them.
Input
The first line is an integer T which shows the number of cases.
Then T cases follow. Each case contains 2 lines:
The first line has 2 integers n and m.
The second line contains n integers.
(0 < n < 11, 0 < m < 2^31, 0 < xi < 100)
Output
For each case print the answer in a single line.
Sample Input
2 1 10 3 2 20 3 5
Sample Output
3 9
Source
题意是给出n个数an,询问1到m至少可以被其中一个数整除的个数
利用容斥原理解,用二进制或DFS确定项
aUbUc...Uz=a+b+c+..+z-anb-anc-......
a=m/ai
anb=lcm(a,b),anbnc..nz=lcm(a,b,c...z)
#include<stdio.h> int gcd(int x,int y) {return y?gcd(y,x%y):x;} int lcm(int x,int y) {return x*y/gcd(x,y);} int main() { int t,n,m,i,k,s,ans,x,p,a[105]; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); ans=0; for(i=0;i<n;i++) scanf("%d",a+i); for(k=1;k<(1<<n);k++) { s=1; for(p=i=0;i<n;i++) if(k>>i&1) { s=lcm(s,a[i]); p++; } x=-m/s; if(p&1) x=-x; ans+=x; } printf("%d/n",ans); } }