poj 1091 跳蚤(容斥原理)
2017-08-20 21:30
309 查看
要使跳蚤可以跳到起始点左边一个单位,要保证卡片上N+1个数字的gcd=1,否则的话,跳蚤跳到距离起始点左边或者右边的距离一定不能躲开他们之间的公因子。这就好求了,就是求有多少种方案使得这N+1个数字的gcd为1,这个并不好求,但是我们可以求有多少种方案使得N+1个数字的gcd!=1。因为M是最大的,所以这个gcd一定是M的因子,我们可以把M素因子分解,然后枚举M的素因子进行容斥。假设gcd=p,则有M/p个数可以选择,前N位数的方案数就是(M/p)n,然后对M的素因子容斥,最后用Mn减掉就是结果了
#include <iostream> using namespace std; typedef __int64 LL; int p[50]; int plen; int n,m; LL pow(LL a, int b) { LL ret = 1; while(b) { if(b&1) ret *= a; a *= a; b >>= 1; } return ret; } void solve() { int tmp = m; for(int i = 2; i <= tmp/i; ++i) { if(tmp%i == 0) { p[plen++] = i; while(tmp%i == 0) tmp /= i; } } if(tmp > 1) p[plen++] = tmp; LL res = 0; LL M = m; for(int i = 1; i < (1<<plen); ++i) { LL mult = 1; int cnt = 0; for(int j = 0; j < plen; ++j) { if(i&(1<<j)) { ++cnt; mult *= p[j]; } } if(cnt&1) res += pow(M/mult,n); else res -= pow(M/mult,n); } cout << pow(M,n)-res << endl; } int main() { cin >> n >> m; solve(); return 0; }
相关文章推荐
- poj 1091 跳蚤(最大公约数原理+容斥原理)
- POJ 1091 跳蚤 容斥原理
- POJ 1091 跳蚤(容斥原理)
- POJ 1091 跳蚤 [容斥原理]【组合数学】
- 跳蚤 - POJ 1091 容斥原理
- poj 1091 跳蚤(组合数学-容斥原理)这题要求的数据范围根本就是胡说八道!
- POJ 1091 跳蚤 解题报告(2014携程第一场A题,聪明的猴子)(容斥原理)
- POJ 1091 跳蚤 数论-容斥原理、扩展欧几里得
- POJ 1091 跳蚤(n元不定方程有解的判定+容斥原理)
- poj 1091 跳蚤 (数论,容斥原理)
- POJ 1091 简单容斥原理(莫比乌斯反演)
- POJ - 1091 跳蚤
- 【POJ】1091 跳蚤
- poj1091跳蚤---------容斥
- poj 1091 (容斥原理)
- POJ 1091 跳蚤
- poj 1091 跳蚤(n元一次不定方程+斥容原理)
- POJ 1091 跳蚤(经过三天的研究,终于真的把它搞明白了)
- poj 1091 跳蚤
- POJ 1091 - 跳蚤 (容斥)