cf900D. Unusual Sequences(容斥 莫比乌斯反演)
题意
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(容斥 莫比乌斯反演)的更多相关文章
- 【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= ...
- 51nod 1355 - 斐波那契的最小公倍数(Min-Max 容斥+莫比乌斯反演)
vjudge 题面传送门 首先我们知道斐波那契数列的 lcm 是不太容易计算的,但是它们的 gcd 非常容易计算--\(\gcd(f_x,f_y)=f_{\gcd(x,y)}\),该性质已在我的这篇博 ...
- 【二分+容斥+莫比乌斯反演】BZOJ2440 完全平方数
Description 求第k个没有完全平方因子的数,k<=1e9. Solution 这其实就是要求第k个µ[i](莫比乌斯函数)不为0的数. 然而k太大数组开不下来是吧,于是这么处理. 二分 ...
- bzoj 2005 & 洛谷 P1447 [ Noi 2010 ] 能量采集 —— 容斥 / 莫比乌斯反演
题目:bzoj 2005 https://www.lydsy.com/JudgeOnline/problem.php?id=2005 洛谷 P1447 https://www.luogu.org/ ...
- HDU 5942 Just a Math Problem 容斥 莫比乌斯反演
题意:\( g(k) = 2^{f(k)} \) ,求\( \sum_{i = 1}^{n} g(i) \),其中\( f(k)\)代表k的素因子个数. 思路:题目意思很简单,但是着重于推导和简化,这 ...
- Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)
题目链接 \(Description\) 给定n个数(\(1\leq a_i\leq 5*10^5\)),每次从这n个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...
- BZOJ4833: [Lydsy1704月赛]最小公倍佩尔数(min-max容斥&莫比乌斯反演)(线性多项式多个数求LCM)
4833: [Lydsy1704月赛]最小公倍佩尔数 Time Limit: 8 Sec Memory Limit: 128 MBSubmit: 240 Solved: 118[Submit][S ...
- HDU 2841 容斥 或 反演
$n,m <= 1e5$ ,$i<=n$,$j<=m$,求$(i⊥j)$对数 /** @Date : 2017-09-26 23:01:05 * @FileName: HDU 284 ...
- 【题解】[HAOI2018]染色(NTT+容斥/二项式反演)
[题解][HAOI2018]染色(NTT+容斥/二项式反演) 可以直接写出式子: \[ f(x)={m \choose x}n!{(\dfrac 1 {(Sx)!})}^x(m-x)^{n-Sx}\d ...
随机推荐
- 记录cacl()函数中使用scss变量不生效的问题
问题 使用cacl()动态计算元素的高度,运算中包含一个scss变量.如下: height: calc(100% - $ws-header-height); 在浏览器中发现并没有达到预期效果,scss ...
- Spring Boot 中使用 Jedis 及 Lettuce的对比
首先,同样的程序,采用不同方式的Redis连接方式. defautl : 默认,0配置 ,也就是走的是 lettuce 单通道方式. 端口:8081 jedis : 使用Jedis 连接池. ...
- dubbo源码阅读之SPI
dubbo SPI SPI,全程Service Provider interface, java中的一种借口扩展机制,将借口的实现类注明在配置文件中,程序在运行时通过扫描这些配置文件从而获取全部的实现 ...
- js 开发过程中经验及总结记录
一 let 和 var 作用域 1 普通用法 for (var i = 0; i < 5; i++) { console.log(i); } console.log(i); //-- ...
- Java之装饰模式
1.装饰模式的理解 在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2.装饰模式由4种角色组成 (1)抽象构件(Componen ...
- FineBI学习系列之FineBI的ETL处理(图文详解)
不多说,直接上干货! 这是来自FineBI官网提供的帮助文档 http://help.finebi.com/http://help.finebi.com/doc-view-48.html 目录: 1. ...
- js便签笔记(10) - 分享:json.js源码解读笔记
1. 如何理解“json” 首先应该意识到,json是一种数据转换格式,既然是个“格式”,就是个抽象的东西.它不是js对象,也不是字符串,它只是一种格式,一种规定而已. 这个格式规定了如何将js对象转 ...
- MyCat配置文件详解--server.xml
server.xml包含mycat的系统配置信息,它有两个标签,分别是user和system,掌握system标签的各项配置属性是mycat调优的关键. <?xml version=" ...
- Disruptor入门
一.什么是 Disruptor Disruptor是一个高性能的异步处理框架,或者可以认为是最快的消息框架(轻量的JMS),也可以认为是一个观察者模式实现,或者事件-监听模式的实现,直接称disrup ...
- async & await 异步编程小示例,一看就懂
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...