ZJU 3233 - Lucky Number (容斥原理)

题意

找出[low, high]之间数的数量,符合下面两个条件。

  1. 至少被一个lucky数整除。
  2. 至少不被一个unLucky数整除。

思路

大思路是用容斥来统计。

  1. 如何统计数的数量? 
    用前缀和的思想。

  2. 如何得出某个数以内符合条件的数量? 
    ans = |条件一| - |条件一 && !条件2|.

就是说答案等于所有符合条件一的数量(这时候里面包含能被全部unlucky整除的数量至少不被一个unlucky整除的数量)减去能被全部unlucky整除的数量

求出unlucky的LCM,然后和往常一样对lucky进行容斥即可。

注意LCM是有可能溢出的,这时候可以看成绝对满足条件2,只要求出条件1的数量即可。

代码

 
 
  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <string>
  13. #include <map>
  14. #include <iomanip>
  15. #include <cmath>
  16. #define LL long long
  17. #define ULL unsigned long long
  18. #define SZ(x) (int)x.size()
  19. #define Lowbit(x) ((x) & (-x))
  20. #define MP(a, b) make_pair(a, b)
  21. #define MS(arr, num) memset(arr, num, sizeof(arr))
  22. #define PB push_back
  23. #define F first
  24. #define S second
  25. #define ROP freopen("input.txt", "r", stdin);
  26. #define MID(a, b) (a + ((b - a) >> 1))
  27. #define LC rt << 1, l, mid
  28. #define RC rt << 1|1, mid + 1, r
  29. #define LRT rt << 1
  30. #define RRT rt << 1|1
  31. #define BitCount(x) __builtin_popcount(x)
  32. #define BitCountll(x) __builtin_popcountll(x)
  33. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  34. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  35. const double PI = acos(-1.0);
  36. const int INF = 0x3f3f3f3f;
  37. using namespace std;
  38. const double eps = 1e-6;
  39. const int MAXN = 1500 + 10;
  40. const int MOD = 1000007;
  41. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  42. typedef pair<int, int> pii;
  43. typedef vector<int>::iterator viti;
  44. typedef vector<pii>::iterator vitii;
  45. LL lucky[MAXN], unlucky[MAXN], lcm, nLuc, nUnluc;
  46. bool flag;
  47. LL LCM(LL a, LL b)
  48. {
  49. return a / __gcd(a, b) * b;
  50. }
  51. LL Solve(LL num)
  52. {
  53. int totState = (1 << nLuc);
  54. LL ans = 0;
  55. for (int curState = 1; curState < totState; curState++)
  56. {
  57. LL pro = 1;
  58. int cnt = 0;
  59. for (int i = 0; i < nLuc; i++)
  60. {
  61. if ((1 << i) & curState)
  62. {
  63. pro = LCM(pro, lucky[i]);
  64. cnt++;
  65. }
  66. }
  67. if (cnt & 1)
  68. {
  69. if (flag) ans += num / pro;
  70. else ans += num / pro - num / LCM(pro, lcm);
  71. }
  72. else
  73. {
  74. if (flag) ans -= num / pro;
  75. else ans -= num / pro - num / LCM(pro, lcm);
  76. }
  77. }
  78. return ans;
  79. }
  80. int main()
  81. {
  82. //ROP;
  83. int i, j;
  84. LL low, high;
  85. while (cin >> nLuc >> nUnluc >> low >> high, nLuc + nUnluc + low + high)
  86. {
  87. lcm = 1; flag = false;
  88. for (j = 0; j < nLuc; j++) cin >> lucky[j];
  89. for (i = 0; i < nUnluc; i++) cin >> unlucky[i];
  90. for (int i = 0; i < nUnluc; i++) lcm = LCM(unlucky[i], lcm);
  91. if (lcm < 0)
  92. {
  93. flag = true;
  94. cout << Solve(high) - Solve(low - 1) << endl;
  95. }
  96. else cout << Solve(high) - Solve(low - 1) << endl;
  97. }
  98. return 0;
  99. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值