【ZOJ】2836 Number Puzzle

本文介绍了一种解决特定整数倍数计数问题的方法,通过使用位运算和数学函数如最大公约数(GCD)及最小公倍数(LCM),实现了一种高效的算法解决方案。

题意:同【HDU】1796 How many integers can you find。

 1 #include<cstdio>
 2 #include<algorithm>
 3 typedef long long LL;
 4 #define MAXN 110
 5 using namespace std;
 6 int m;
 7 LL n, ans;
 8 int a[MAXN];
 9 LL GCD(LL x, LL y) {
10     return y ? GCD(y, x % y) : x;
11 }
12 LL LCM(LL x, LL y) {
13     return x / GCD(x, y) * y;
14 }
15 LL Get(int x, int &cnt) {
16     int i;
17     LL res = 1;
18     for (i = cnt = 0; i < m; i++) {
19         if (x & (1 << i)) {
20             cnt++;
21             res = LCM(res, a[i]);
22         }
23     }
24     return res;
25 }
26 int main() {
27     int i, k;
28     LL tmp;
29     while (~scanf("%d%lld", &m, &n)) {
30         for (ans = i = 0; i < m; i++)
31             scanf("%d", &a[i]);
32         for (i = 1; i < (1 << m); i++) {
33             tmp = Get(i, k);
34             if (k & 1)
35                 ans += n / tmp;
36             else
37                 ans -= n / tmp;
38         }
39         printf("%lld\n", ans);
40     }
41     return 0;
42 }

转载于:https://www.cnblogs.com/DrunBee/archive/2012/09/05/2672646.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值