题意

题目链接

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

  1. #include<bits/stdc++.h>
  2. #define Pair pair<int, int>
  3. #define MP(x, y) make_pair(x, y)
  4. #define fi first
  5. #define se second
  6. #define int long long
  7. #define LL long long
  8. #define Fin(x) {freopen(#x".in","r",stdin);}
  9. #define Fout(x) {freopen(#x".out","w",stdout);}
  10. //#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1<<22, stdin), p1 == p2) ? EOF : *p1++)
  11. //char buf[(1 << 22)], *p1 = buf, *p2 = buf;
  12. using namespace std;
  13. const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
  14. const double eps = 1e-9;
  15. template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
  16. template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
  17. 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;}
  18. 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);}
  19. template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
  20. template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
  21. template <typename A> inline void debug(A a){cout << a << '\n';}
  22. template <typename A> inline LL sqr(A x){return 1ll * x * x;}
  23. inline int read() {
  24. char c = getchar(); int x = 0, f = 1;
  25. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  26. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  27. return x * f;
  28. }
  29. int x = read(), y = read();
  30. map<int, int> mu;
  31. int g(int a, int p) {
  32. int base = 1;
  33. while(p) {
  34. if(p & 1) base = mul(base, a);
  35. p >>= 1; a = mul(a, a);
  36. }
  37. return base;
  38. }
  39. signed main() {
  40. if(y % x != 0) return puts("0"), 0;
  41. vector<int> d; y /= x; int p = y;
  42. for(int i = 2; i * i <= y; i++)
  43. if(!(y % i)) {
  44. d.push_back(i);
  45. while(!(y % i)) y /= i;
  46. }
  47. if(y != 1) d.push_back(y);
  48. y = p;
  49. for(int sta = 0; sta < (1 << d.size()); sta++) {
  50. int v = 1, t = 1;
  51. for(int i = 0; i < d.size(); i++) if(sta & (1 << i)) t *= -1, v *= d[i];
  52. mu[v] = t;
  53. }
  54. int ans = 0;
  55. for(auto &x: mu) {
  56. int d = x.fi, m = x.se;
  57. add2(ans, mul(m + mod, g(2, y / d - 1)));
  58. }
  59. cout << ans;
  60. return 0;
  61. }

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. 记录cacl()函数中使用scss变量不生效的问题

    问题 使用cacl()动态计算元素的高度,运算中包含一个scss变量.如下: height: calc(100% - $ws-header-height); 在浏览器中发现并没有达到预期效果,scss ...

  2. Spring Boot 中使用 Jedis 及 Lettuce的对比

    首先,同样的程序,采用不同方式的Redis连接方式. defautl : 默认,0配置 ,也就是走的是 lettuce 单通道方式.   端口:8081 jedis : 使用Jedis 连接池.    ...

  3. dubbo源码阅读之SPI

    dubbo SPI SPI,全程Service Provider interface, java中的一种借口扩展机制,将借口的实现类注明在配置文件中,程序在运行时通过扫描这些配置文件从而获取全部的实现 ...

  4. js 开发过程中经验及总结记录

    一   let 和 var 作用域    1  普通用法 for (var i = 0; i < 5; i++) { console.log(i); } console.log(i); //-- ...

  5. Java之装饰模式

    1.装饰模式的理解 在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2.装饰模式由4种角色组成 (1)抽象构件(Componen ...

  6. FineBI学习系列之FineBI的ETL处理(图文详解)

    不多说,直接上干货! 这是来自FineBI官网提供的帮助文档 http://help.finebi.com/http://help.finebi.com/doc-view-48.html 目录: 1. ...

  7. js便签笔记(10) - 分享:json.js源码解读笔记

    1. 如何理解“json” 首先应该意识到,json是一种数据转换格式,既然是个“格式”,就是个抽象的东西.它不是js对象,也不是字符串,它只是一种格式,一种规定而已. 这个格式规定了如何将js对象转 ...

  8. MyCat配置文件详解--server.xml

    server.xml包含mycat的系统配置信息,它有两个标签,分别是user和system,掌握system标签的各项配置属性是mycat调优的关键. <?xml version=" ...

  9. Disruptor入门

    一.什么是 Disruptor Disruptor是一个高性能的异步处理框架,或者可以认为是最快的消息框架(轻量的JMS),也可以认为是一个观察者模式实现,或者事件-监听模式的实现,直接称disrup ...

  10. async & await 异步编程小示例,一看就懂

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...