SOJ 3137: Simple Computing _容斥原理
2012-02-28 22:11
363 查看
Time Limit: 2000 MS Memory Limit: 65536 K
Description
Given n integers x1 x2 ... xn, you should count how many intergers from 1 to m that can be divided by at least one of them.Input
The first line is an integer c which shows the number of cases. 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
21 1022 203 4Sample Output
510Source
The Star of ACMClub 2007 @ HIT//题意,给出n个数,求1到m中有多少个数(假设这个数是i)满足n个数中至少有一个数能够整除i;
//变形的求1~n中与m互质的数的个数问题!这里将这n个数直接当做是n个素因子,可以产生多种组合,不断筛选出能够整除1~m中得数的个数,容斥!注意的是,假如n中有两个数3和6,而m=18,对于3*6|18,计算值是1,但是12显然也能够被3、6、3*6整除,所以需要求出这k个数的最小公倍数(需要用long long,没注意错了几次)。
#include<stdio.h> #include<string.h> using namespace std; #define LL long long #define maxn 15 int a[maxn]; int gcd(int a,int b) { int c; while(b) { c=b; b=a%b; a=c; } return a; } int make_ans(int n,int m) { int ans=0,i,j,flag; LL tmp; for(i=1;i<(1<<n);i++) { tmp=1,flag=0; for(j=0;j<n&&tmp<=m;j++) if(i&(1<<j)) flag++,tmp=(LL)(a[j]/gcd(tmp,a[j]))*tmp; if(flag&1) ans+=m/tmp; else ans-=m/tmp; } return ans; } int main() { int t,n,m,i; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("%d\n",make_ans(n,m)); } return 0; }
相关文章推荐
- soj 3137 Simple Computing 容斥原理 hdu 1796 How many integers can you find
- GCD 容斥原理
- The Boss on Mars(容斥原理)
- Coprime (二分+容斥原理)
- 容斥原理
- UVa 10325 The Lottery 容斥原理
- Hdu 5212 Code【容斥原理】
- hdu 4336 容斥原理 2012 Multi-University Training Contest 4
- UVA 10721 Bar Codes(容斥原理)
- HYSBZ-1853 幸运数字(容斥原理,dfs)
- qwb与支教(二分+容斥原理)
- UVAlive7040(组合数,容斥原理,逆元)
- 错排问题 组合数学+容斥原理
- hdu 4135(容斥原理)
- BZOJ 2839: 集合计数 容斥原理 组合数学
- hdu1796 How many integers can you find (容斥原理)
- POJ2407_Relatives_容斥原理
- zoj 3547(容斥原理)
- 容斥原理(翻译)
- 组合数学——容斥原理和错位排列