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. 社区v2022.3版上线:发帖、看直播、评论体验升级,并支持机构号申请
  2. 你的微服务实践还顺利吗?
  3. Java wait forever_彻底搞清楚Java并发 (一) 基础
  4. 阿里云三维可视化使用初体验
  5. 我的世界有宠物系统的服务器,我的世界1.6.4宠物战争服务器客户端
  6. MySql分页存储过程
  7. GHOST系统锁定主页常用软件及解决方案
  8. php socket 基础知识
  9. 财政部:瑞幸咖啡虚增收入21.19亿元 虚增利润9.08亿元
  10. java线程——详解Callable、Future和FutureTask
  11. 【FinE】期权定价理论(1)
  12. hp台式计算机怎么进bois,HP笔记本怎么进BIOS?惠普电脑进入BIOS方法
  13. 10bit、8bit色彩深度的区别
  14. P1129 矩阵游戏
  15. 谷歌表格_如何计算Google表格中的空白或空单元格
  16. 洗地机怎么选?洗地机品牌排行榜
  17. 计算机安装不了cad2007,win10系统电脑中安装cad2007不兼容无法打开的还原方法
  18. linux sli 提高效率,两大优点!NVIDIA混合SLI功能完全解析
  19. 基于JAVA和MYSQL数据库实现的图书资料管理信息系统
  20. 信号频域分析的一点理解

热门文章

  1. 初级考试可以使用计算机吗,计算机初级考试的内容都有哪些
  2. 计算机图形学浙江大学第一章图形学概述
  3. VRTK4开发VR2:射线
  4. SM30 标准程序 添加事件、checkbox 等客制化需求
  5. 如何快速读Paper
  6. 如何求矩阵的若儿当标准型
  7. Netbeans 常用快捷键整理
  8. 行业竞争分析及发展动向
  9. rt-thread 学习(二)外部中断 添加头文件 延时函数
  10. linux的echo连接字符串输出,echo命令 – 输出字符串或提取She