题意

题目链接

Sol

首先若y % x不为0则答案为0

否则,问题可以转化为,有多少个数列满足和为y/x,且整个序列的gcd=1

考虑容斥,设\(g[i]\)表示满足和为\(i\)的序列的方案数,显然\(g[i] = 2^{i-1}\)(插板后每空位放不放)

同时还可以枚举一下gcd,设\(f[i]\)表示满足和为\(i\)且所有数的gcd为1的方案,\(g[i] = \sum_{d | i} f[\frac{n}{d}]\)

反演一下,\(f[i] = \sum_{d | i} \mu(d) g(\frac{i}{d})\)

mu函数可以暴力枚举质因子得到

复杂度\(O(2^{Mx} * Mx + \sqrt{N}\) ,\(Mx\)最大为10

#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 22)], *p1 = buf, *p2 = buf;
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int x = read(), y = read();
map<int, int> mu;
int g(int a, int p) {
int base = 1;
while(p) {
if(p & 1) base = mul(base, a);
p >>= 1; a = mul(a, a);
}
return base;
}
signed main() {
if(y % x != 0) return puts("0"), 0;
vector<int> d; y /= x; int p = y;
for(int i = 2; i * i <= y; i++)
if(!(y % i)) {
d.push_back(i);
while(!(y % i)) y /= i;
}
if(y != 1) d.push_back(y);
y = p;
for(int sta = 0; sta < (1 << d.size()); sta++) {
int v = 1, t = 1;
for(int i = 0; i < d.size(); i++) if(sta & (1 << i)) t *= -1, v *= d[i];
mu[v] = t;
}
int ans = 0;
for(auto &x: mu) {
int d = x.fi, m = x.se;
add2(ans, mul(m + mod, g(2, y / d - 1)));
}
cout << ans;
return 0;
}

cf900D. Unusual Sequences(容斥 莫比乌斯反演)的更多相关文章

  1. 【CF900D】Unusual Sequences 容斥(莫比乌斯反演)

    [CF900D]Unusual Sequences 题意:定义正整数序列$a_1,a_2...a_n$是合法的,当且仅当$gcd(a_1,a_2...a_n)=x$且$a_1+a_2+...+a_n= ...

  2. 51nod 1355 - 斐波那契的最小公倍数(Min-Max 容斥+莫比乌斯反演)

    vjudge 题面传送门 首先我们知道斐波那契数列的 lcm 是不太容易计算的,但是它们的 gcd 非常容易计算--\(\gcd(f_x,f_y)=f_{\gcd(x,y)}\),该性质已在我的这篇博 ...

  3. 【二分+容斥+莫比乌斯反演】BZOJ2440 完全平方数

    Description 求第k个没有完全平方因子的数,k<=1e9. Solution 这其实就是要求第k个µ[i](莫比乌斯函数)不为0的数. 然而k太大数组开不下来是吧,于是这么处理. 二分 ...

  4. bzoj 2005 & 洛谷 P1447 [ Noi 2010 ] 能量采集 —— 容斥 / 莫比乌斯反演

    题目:bzoj 2005 https://www.lydsy.com/JudgeOnline/problem.php?id=2005   洛谷 P1447 https://www.luogu.org/ ...

  5. HDU 5942 Just a Math Problem 容斥 莫比乌斯反演

    题意:\( g(k) = 2^{f(k)} \) ,求\( \sum_{i = 1}^{n} g(i) \),其中\( f(k)\)代表k的素因子个数. 思路:题目意思很简单,但是着重于推导和简化,这 ...

  6. Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)

    题目链接 \(Description\) 给定n个数(\(1\leq a_i\leq 5*10^5\)),每次从这n个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...

  7. BZOJ4833: [Lydsy1704月赛]最小公倍佩尔数(min-max容斥&莫比乌斯反演)(线性多项式多个数求LCM)

    4833: [Lydsy1704月赛]最小公倍佩尔数 Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 240  Solved: 118[Submit][S ...

  8. HDU 2841 容斥 或 反演

    $n,m <= 1e5$ ,$i<=n$,$j<=m$,求$(i⊥j)$对数 /** @Date : 2017-09-26 23:01:05 * @FileName: HDU 284 ...

  9. 【题解】[HAOI2018]染色(NTT+容斥/二项式反演)

    [题解][HAOI2018]染色(NTT+容斥/二项式反演) 可以直接写出式子: \[ f(x)={m \choose x}n!{(\dfrac 1 {(Sx)!})}^x(m-x)^{n-Sx}\d ...

随机推荐

  1. SQL高效查询两个表不同的数据

    逻辑相对复杂,但是速度最快: )

  2. 【sping揭秘】5、IOC容器(一)

    OC容器实现过程 1. 容器启动阶段,读取配置文件,解析文件BeanDefinitionReader 2. Bean 实例化阶段 关于BeanFactoryPostProcessor 插手spring ...

  3. 执行HBase shell时出现ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet错误解决办法(图文详解)

    不多说,直接上干货! [kfk@bigdata-pro01 bin]$ jps NameNode ResourceManager JournalNode HMaster DataNode HRegio ...

  4. 优先队列/oriority queue 之最大优先队列的实现

    优先队列(priority queue)是一种用来维护一组数据集合S的数据结构.每一个元素都有一个相关的值,被称为关键字key. 这里以实现最大优先队列为例子 最大优先队列支持的操作如下:INSERT ...

  5. Java 容器之 Connection栈队列及一些常用

    集合家族图 ---|Collection: 单列集合 ---|List: 有存储顺序 , 可重复 ---|ArrayList: 数组实现 , 查找快 , 增删慢 ---|LinkedList: 链表实 ...

  6. Hibernate关联关系映射之一对多双向映射

    一对多映射有两种,一种是单向的,另一种的多向.我们一般是使用双向的,所以我就写写一对多的双向映射. 还是想昨天一样举个例子来说明:作者<===>作品,还是对数据进行增删改查. 我们一般是把 ...

  7. Autowired使用说明

    使用了那么久Spring,一下子问我Autowired注解使用条件,答不上来吧,看了Spring源码,一点点收货: 废话少说,要是Autowired生效,需要注册BeanPostProcessor,你 ...

  8. org.apache.commons.lang.StringUtils的常用方法

    org.apache.commons.lang.StringUtils是apache的commons-lang-x.x.jar下的包,里面包含很多字符串操作方法, 官网(http://commons. ...

  9. Android系统版本、Platform版本、SDK版本、gradle修改

    虽然之前分析了gradle,但是在eclipse导入Android studio的时候,各个版本出现的问题还是很模糊,下面对各种版本进行一下说明: 参考资料: https://developer.an ...

  10. .37-浅析webpack源码之事件流make(4)

    赶紧完结这个系列咯,webpack4都已经出正式版了. 之前的代码搜索到js文件的对应loader,并添加到了对象中返回,流程如下: this.plugin("factory", ...