Codeforces Round #450 (Div. 2) D. Unusual Sequences 莫比乌斯系数容斥
来源:互联网 发布:网络海豚壁纸图片 编辑:程序博客网 时间:2024/05/16 08:18
D. Unusual Sequences
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Count the number of distinct sequences a1, a2, …, an (1 ≤ ai) consisting of positive integers such that gcd(a1, a2, …, an) = x and . As this number could be large, print the answer modulo 109 + 7.
gcd here means the greatest common divisor.
Input
The only line contains two positive integers x and y (1 ≤ x, y ≤ 109).
Output
Print the number of such sequences modulo 109 + 7.
Examples
input
3 9
output
3
input
5 8
output
0
题意:构造一个数组,要求每一位大于等于1,所有数的gcd为1,就相当于构造y/x,gcd为1的序列,首先我们可以证明对于n,把它任意的分成一个数组,一共有2^(n-1)种,然后可以知道这些序列的gcd是某一个数a的倍数就是2^(n/a-1)种,然后用莫比乌斯系数容斥就可以得到答案了。因为对于1e9的数据来说,最多只有11个质因数,那么可以用状压处理处需要的莫比乌斯系数的值,然后可以得到答案。
#include<bits/stdc++.h>#define ll long longusing namespace std;vector<int> pe;vector<int> pr;bool vis[100005];const int mod = 1e9+7;int x,y;ll qpow(int x,int b){ ll now = x; ll sum =1; while(b){ if(b&1) sum = sum*now%mod; now = now*now%mod; b >>= 1; } return sum;}void init(){ vis[1] = true; for(int i = 2;i <= 100005;i ++){ if(vis[i] == false) pe.push_back(i); for(int j = 0;j < pe.size()&&i*pe[j] < 100005;j ++){ vis[i*pe[j]] = true; if(i%pe[j] == 0) break; } } int now = x; for(int i =0;i < pe.size();i ++){ if(now%pe[i] == 0){ pr.push_back(pe[i]); while(now%pe[i]==0) now /= pe[i]; } } if(now != 1) pr.push_back(now); int res = pr.size(); ll ans = qpow(2,x-1); for(int i = 1;i < (1<<res);i ++){ int tmp = 1,f = 1; for(int j = 0;j <res;j ++){ if(i&(1<<j)) tmp *= pr[j],f *= -1; } ans = (ans+qpow(2,x/tmp-1)*f+mod)%mod; } printf("%lld\n",ans);}int main(){ scanf("%d %d",&x,&y); if(y%x){ puts("0"); return 0; } x = y/x; init(); return 0;}
- Codeforces Round #450 (Div. 2) D. Unusual Sequences 莫比乌斯系数容斥
- Codeforces 900D-Unusual Sequences
- Codeforces Round #162 (Div. 2), problem: (D) Good Sequences
- CF#450 D.Unusual Sequences
- codeforces 900D Unusual Sequences (数论)
- Codeforces Round #238 (Div. 2)C:Unusual Product
- Codeforces Round #428 (Div. 2) D. Winter is here(容斥,补题)
- Codeforces Round #428 (Div. 2) B 细节 D gcd预处理因子+容斥
- Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式) +容斥
- codeforces 839D (推公式+容斥原理/莫比乌斯函数)
- CF 900D Unusual Sequence 容斥
- 解题报告: Codeforces Round #305 (Div. 2) E.Mike and Foam (莫比乌斯反演)
- codeforces Unusual Sequences (数论)
- 839D(容斥 || 莫比乌斯反演)
- Codeforces Round #FF (Div. 2):C. DZY Loves Sequences
- Codeforces Round #FF (Div. 2) Problem C DZY Loves Sequences
- Codeforces Round #FF (Div. 2)C - DZY Loves Sequences
- Codeforces Round #FF (Div. 2)-C. DZY Loves Sequences
- MFC-UpdateData(false)和UpdateData(true)的区别
- CefSharp 集成谷歌浏览器详解(四)--官网示例解析3 RegisterJsObject CefSharpSchemeHandlerFactory
- 《剑指offer》刷题笔记(知识迁移能力):数字在排序数组中出现的次数
- 手动安装m4, autoconf, automake, libtool
- Android中View测量、布局及绘制原理
- Codeforces Round #450 (Div. 2) D. Unusual Sequences 莫比乌斯系数容斥
- git统计操作
- 编译android源码学习的First Step
- Plugin execution not covered by lifecycle configuration
- Java的三种代理模式
- TensorFlow实现深度学习算法的教程汇集:代码+笔记
- java的会话管理:Cookie和Session
- 神经网络中的激活函数具体是什么?为什么ReLu要好过于tanh和sigmoid function?
- Problem8:String to Integer (atoi)