D. Unusual Sequences
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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

Copy

3 9

output

Copy

3

input

Copy

5 8

output

Copy

0

Note

There are three suitable sequences in the first test: (3, 3, 3), (3, 6), (6, 3).

There are no suitable sequences in the second test.

题意:构造数列使得gcd(a1,a2,...,an)=n && a1+a2+...+an=m 询问共有多少种构造方法。

题解:首先可知m%n必须为0,否则无法构造,此时可分出m/n个n,利用隔板法可得共有qpow(2,m/n-1)种方案,然后再将 gcd==k*n 的方案减去即可。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e6+10;
const long long INF=0x7FFFFFFFFFFFFFFFLL;
const int inf=0x3f3f3f3f;
typedef long long ll;
using namespace std;
const int mod=1e9+7;ll qpow(ll a,ll b)
{ll ans=1;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod;b/=2;}return ans;
}ll dp[10005];
int main()
{ll n,m;while(cin>>n>>m){vector<ll>v;if(m%n){printf("0\n");continue;}for(ll i=1;i<=sqrt(m);i++) //将m的因子中为n的倍数的数找出来{if(m%i==0){if(i%n==0) v.push_back(i);if(i*i!=m && m/i%n==0) v.push_back(m/i);}}sort(v.begin(),v.end());for(int i=v.size()-1;i>=0;i--){dp[i]=qpow(2,m/v[i]-1); //构造gcd为(i+1)*n的序列的总方案数for(int j=i+1;j<v.size();j++) //dp递推过去if(v[j]%v[i]==0)dp[i]=(dp[i]-dp[j]+mod)%mod; //构造gcd为x的需要减去gcd为2*x,3*x....,所以倒着递推}printf("%I64d\n",dp[0]);}return 0;
}

D. Unusual Sequences (数论,质因子分解,dp)相关推荐

  1. Codeforces Round #450 (Div. 2)D. Unusual Sequences[数论][组合数学][dp II]

    题目:http://codeforces.com/contest/900/problem/D 题意:找到加和为m的且gcd为n的数列种类数 分析:可以转化为求gcd为1的加和为m/n的种类数,假设有m ...

  2. 【数论】 质数知识总结(质数判断、筛选、质因子分解、互质)

    文章目录 一.定义 二.质数的判断 三.质数的筛选 四.质因子分解 五.互质 一.定义 质数,又称素数,若一个正整数无法被除了1和它自身以外的其它数整除,则称其为质数,否则为合数.特殊地,1既不是合数 ...

  3. codeforces900D Unusual Sequences

    D. Unusual Sequences time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Lightoj-1356 Prime Independence(质因子分解)(Hopcroft-Karp优化的最大匹配)

    题意: 找出一个集合中的最大独立集,任意两数字之间不能是素数倍数的关系. 思路: 最大独立集,必然是二分图. 最大数字50w,考虑对每个数质因子分解,然后枚举所有除去一个质因子后的数是否存在,存在则建 ...

  5. 2021年中国大学生程序设计竞赛 女生专场 - 热身赛 Problem C. 口算训练(质因子分解)

    题目分析:判断 xxx 是 yyy 的倍数,等价于质因子分解后, yyy 中的每个质因子的出现次数都小于等于其在 xxx 中的出现次数. 那么对于每次询问 [l,r,d][l,r,d][l,r,d], ...

  6. CodeForces - 1419E Decryption(质因子分解+构造)

    题目链接:点击查看 题目大意:给出一个数 n ,现在需要将其所有大于 1 的因子重新排列成一个首尾相接的环,规定每次操作可以在相邻两个位置中加入这两个数的 lcm,问最少需要操作多少次,才能使得环上所 ...

  7. 牛客多校9 - Groundhog Chasing Death(质因子分解+思维)

    题目链接:点击查看 题目大意:给出 a , b , c , d , x , y ,求 题目分析:因为涉及到了 gcd 的乘积运算,那么易知不同质因子的贡献是相互独立的,首先我们就可以先将 x 和 y ...

  8. 洛谷——P2043 质因子分解

    P2043 质因子分解 题目描述 对N!进行质因子分解. 输入格式 输入数据仅有一行包含一个正整数N,N<=10000. 输出格式 输出数据包含若干行,每行两个正整数p,a,中间用一个空格隔开. ...

  9. python质因子分解_质因子分解_个人文章 - SegmentFault 思否

    质因子分解的问题就是给定一个n使得n能够分解为多个因子的乘积形式,并且相同因子用指数形式表示: 例如180=2^23^25; 对于这个问题,很好理解,我们的目的就是寻找其因子,通常的方法也就是从0开始 ...

最新文章

  1. 第三章 “我要点爆”微信小程序云开发之点爆方式页面和爆炸之音页面制作
  2. 剑指offer-包含min函数的栈
  3. JPA / Hibernate:基于版本的乐观并发控制
  4. 【JS 逆向百例】某空气质量监测平台无限 debugger 以及数据动态加密分析
  5. 【ZOJ - 4020 】Traffic Light (bfs,分层图)
  6. Android花屏分析,Unity游戏在手机上运行时的花屏现象
  7. SAP License:一个资深SAP顾问的2021年项目总结
  8. DIV+CSS布局总结
  9. NLP --- 产生式模型VS判别式模型
  10. mysql innodb启动失败_关于mysql innodb启动失败无法重启的处理方法讲解
  11. Java中List和ArrayList的区别
  12. 小米蓝牙耳机驱动_硬核拆解——小米蓝牙耳机
  13. DOS命令批量更改文件名称(含空格等)
  14. 动态ip和静态ip的区别
  15. 如何选择关键词以及关键词分析优化
  16. LLVM IR / LLVM指令集入门
  17. 利用 Python 读写文本内容
  18. Myshell AI:让你轻松提高英语口语和听力的神器
  19. UDAF和UDF的介绍
  20. SpringBoot + Vue 实现基于 WebSocket 的聊天室(单聊)

热门文章

  1. 【Ybt OJ】[数学基础 第3章] 同余问题
  2. PlotJuggler的安装遇到的问题记录
  3. android 键盘按下事件,Android虚拟键盘上下左右键按下和弹起的响应事件
  4. ubuntu中把软件放在桌面
  5. 小区隔音墙生产厂家@初心不改,慢煮岁月
  6. MP4/MOV/3GP文件的“ftyp”
  7. Tomcat配置问题(踩坑记录)
  8. 攀登者张梁将出席深圳户外展宣传登山文化,讲述18年登山探险史
  9. 复习一下,? extends T 和 ? super T
  10. 超过百万的数组——解决方法