hdu 1796 hdu 4135 容斥原理
来源:互联网 发布:广电网络客服电话天津 编辑:程序博客网 时间:2024/06/05 01:23
hdu 1796 How many integers can you find
題意
給出一個數字n和一個整數集合S,問比n小的數中,共有多少個數可以被集合S中的若干元素整除。
思路
換個意思就是在
code
#include <bits/stdc++.h>using namespace std;long long n, m;long long a[22];long long ans = 0;long long __lcm (int a, int b) { return a / __gcd(a, b) * b;}long long _get (int _s) { int _c = 0; long long _r = 1; for (int i=0; i<m; i++) { if (((_s >> i) & 1) == 0) continue; _c++; _r = __lcm(_r, a[i]); } if (_c % 2 == 0) return -(n-1) / _r; else return (n-1) / _r; }void _solve () { ans = 0; for (int _s = 1; _s < (1<<m); _s++) { ans += _get(_s); } }int main () { for (; scanf ("%I64d%I64d", &n, &m) == 2; ) { for (int i=0; i<m; i++) { scanf("%I64d", &a[i]); if (a[i] == 0) {i--; m--;} } _solve(); printf ("%I64d\n", ans); } return 0;}
hdu 4135 Co-prime
題意
求[A, B]之間有多少個數字於N互素。
思路
對N進行素因子分解。得到素因子p[],在[A,B]中p[i]的倍數一定不是於N互素。於是我們對p[]容斥就行。
code
#include <bits/stdc++.h>using namespace std;long long a, b, n;long long ans = 0;long long _d[13];int _c;void _div() { _c = 0; memset(_d, 0, sizeof(_d)); for (int i=2; i*i <= n; i++) { if (n % i != 0) continue; _d[_c++] = (long long)i; while (n % i == 0) n /= i; } if (n != 1) { _d[_c++] = n; }}long long _get(int _s) { int _cnt = 0, _res = 1; for (int i=0; i<_c; i++) { if (((_s >> i) & 1) == 0) continue; _cnt++; _res *= _d[i]; } if (_cnt % 2 == 1) return b/_res - (a - 1)/_res; else return (a - 1)/_res - b/_res;}void _solve() { _div(); ans = 0; for (int _s=1; _s < (1<<_c); _s++) { ans += _get(_s); } ans = b - a + 1L - ans; }int main() { int T; scanf ("%d", &T); for (int cases = 1; cases <= T; cases++) { scanf ("%I64d%I64d%I64d", &a, &b, &n); _solve(); printf ("Case #%d: %I64d\n", cases, ans); } return 0;}
0 0
- hdu 1796 hdu 4135 容斥原理
- 【容斥原理】HDU 4135
- hdu 4135 容斥原理
- hdu 4135 (容斥原理)
- HDU 4135 容斥原理
- hdu 4135 容斥原理
- hdu 4135 容斥原理
- hdu 4135(容斥原理)
- hdu 4135 容斥原理
- HDU 4135 容斥原理
- hdu 4135 容斥原理
- hdu 4135(容斥原理)
- HDU 1796 容斥原理
- hdu 1796 容斥原理
- hdu 1796(容斥原理)
- HDU 1796 容斥原理
- hdu 1796 容斥原理
- hdu 1796容斥原理
- ACdream 1007【快速幂+乘法取模】
- UUID of GATT service
- Java集合Map接口与Map.Entry学习
- 使用MediaRecorder类实现录音的功能
- 检查版本更新的方法
- hdu 1796 hdu 4135 容斥原理
- Android/Sdk/build-tools/22.0.1/aapt'' finished with non-zero exit value 1
- Spring Security教程(8)---- 自定义决策管理器及修改权限前缀
- js 如何将某个属性设置成隐藏的
- http2的调试方法
- 【线段树-区间求最值】HDOJ Billboard 2795
- 玩转Visual Studio了解vc++各种类型的工程
- 解决Android Studio卡在building *** gradle project info
- Spring Security教程(9)---- 自定义AccessDeniedHandler