upc组队赛16 GCDLCM 【Pollard_Rho大数质因数分解】
GCDLCM
题目链接
题目描述
In FZU ACM team, BroterJ and Silchen are good friends, and they often play some interesting games.
One day they play a game about GCD and LCM. firstly BrotherJ writes an integer A and Silchen writes an integer B on the paper. Then BrotherJ gives Silchen an integer X. Silchen will win if he can find two integers Y1 and Y2 that satisfy the following conditions:
• GCD(X, Y1) = A
• LCM(X, Y2) = B
• Fuction GCD(X, Y ) means greatest common divisor between X and Y .
• Fuction LCM(X, Y ) means lowest common multiple between X and Y .
BrotherJ loves Silchen so much that he wants Silchen to win the game. Now he wants to calculate how many number of X he can give to Silchen.
输入
Input is given from Standard Input in the following format:
输出
Print one integer denotes the number of X.
样例输入
3
12
样例输出
3
题解
大数质因数分解 Pollard_Rho
代码
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define scac(x) scanf("%c",&x)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pri3(x,y,z) printf("%d %d %d\n",x,y,z)
#define prl(x) printf("%lld\n",x)
#define prl2(x,y) printf("%lld %lld\n",x,y)
#define prl3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
#define mst(x,y) memset(x,y,sizeof(x))
#define ll long long
#define LL long long
#define pb push_back
#define mp make_pair
#define P pair<double,double>
#define PLL pair<ll,ll>
#define PI acos(1.0)
#define eps 1e-6
#define inf 1e17
#define mod 1e9+7
#define INF 0x3f3f3f3f
#define N 1005
const int maxn = 10005;
typedef unsigned long long ULL;
LL Fac[1000];
int Fn;
const int P_TEST[7]={2,3,5,7,11,13,17};
const int RhoLimit=10000;
const int RhoC=13;
LL GetPow(LL x,LL k,LL MOD);
LL gcd(LL x,LL y);
bool Miller_Rabin(LL x);
LL Pollard_Rho(LL n);
void Work(LL n);
LL Random(LL x);
LL QMMul(LL x,LL y,LL MOD);
LL GetPow(LL x,LL k,LL MOD)
{
LL ans=1;
for (;k>0;k>>=1,x=QMMul(x,x,MOD))
if (k&1) ans=QMMul(ans,x,MOD);
return ans;
}
bool Miller_Rabin(LL x)
{
if (x==2) return 1;
if (x<=1 || x&1==0) return 0;
for (int i=0;i<7 && P_TEST[i]<x;++i)
{
LL y=x-1;
while ((y&1)==0) y>>=1;
LL t=GetPow(P_TEST[i],y,x);
while (y!=x-1 && t!=1 && t!=x-1)
{
t=QMMul(t,t,x);
y<<=1;
}
if (!(t==x-1 || (y&1) && t==1)) return 0;
}
return 1;
}
LL Pollard_Rho(LL n)
{
LL x=Random(n-2)+2;
LL y=x;
int STEP=2;
for (int i=1;;++i)
{
x=QMMul(x,x,n)+RhoC;
if (x>=n) x-=n;
if (x==y) return -1;
LL d=gcd(abs(x-y),n);
if (d>1) return d;
if (i==STEP)
{
i=0;
y=x;
STEP<<=1;
}
}
}
void Work(LL n)
{
if (Miller_Rabin(n)) {Fac[Fn++]=n;return;}
LL p;
for (int i=0;i!=RhoLimit;++i)
{
p=Pollard_Rho(n);
if (p!=-1) break;
}
if (p==-1) return;
Work(p);
Work(n/p);
}
LL Random(LL x)
{
unsigned long long p=rand()*rand();
p*=rand()*rand();p+=rand();
return p%x;
}
LL gcd(LL x,LL y)
{
if (y==0) return x;
return gcd(y,x%y);
}
LL QMMul(LL x,LL y,LL MOD)
{
LL ans=0;
for (;y>0;y>>=1)
{
if (y&1)
{
ans+=x;
if (ans>=MOD) ans-=MOD;
}
x+=x;
if (x>=MOD) x-=MOD;
}
return ans;
}
int main()
{
ll a,b;
scanf("%lld%lld",&a,&b);
if(b%a)
{
printf("0\n");
return 0;
}
ll xxx = b/a;
if(xxx == 1)
{
printf("1\n");
return 0;
}
Work(xxx);
sort(Fac+0,Fac+Fn);
ll cnt = 1;
ll temp = Fac[0];
ll res = 1;
for (int i=1;i<Fn;++i)
{
if(temp != Fac[i])
{
res*=(cnt+1);
cnt = 1;
temp = Fac[i];
//cout<<cnt<<endl;
}
else
{
cnt++;
}
}
res *= (cnt+1);
prl(res);
return 0;
}
upc组队赛16 GCDLCM 【Pollard_Rho大数质因数分解】的更多相关文章
- upc组队赛16 Winner Winner【位运算】
Winner Winner 题目链接 题目描述 The FZU Code Carnival is a programming competetion hosted by the ACM-ICPC Tr ...
- upc组队赛16 Melody【签到水】
Melody 题目描述 YellowStar is versatile. One day he writes a melody A = [A1, ..., AN ], and he has a sta ...
- upc组队赛16 WTMGB【模拟】
WTMGB 题目链接 题目描述 YellowStar is very happy that the FZU Code Carnival is about to begin except that he ...
- Pollard_Rho大数分解模板题 pku-2191
题意:给你一个数n, 定义m=2k-1, {k|1<=k<=n},并且 k为素数; 当m为合数时,求分解为质因数,输出格式如下:47 * 178481 = 8388607 = ( ...
- poj 2429 Pollard_rho大数分解
先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里能够直接搜索,注意一个问题,因为同样因子不能分配给两边(会改变gcd)所以能够将同样因子合并,这种话,搜 ...
- 模板题Pollard_Rho大数分解 A - Prime Test POJ - 1811
题意:是素数就输出Prime,不是就输出最小因子. #include <cstdio> #include<time.h> #include <algorithm> ...
- pollard_rho 算法进行质因数分解
//************************************************ //pollard_rho 算法进行质因数分解 //*********************** ...
- 质因数分解的rho以及miller-rabin
一.前言 质因数分解,是一个在算法竞赛里老生常谈的经典问题.我们在解决许多问题的时候需要用到质因数分解来辅助运算,而且质因数分解牵扯到许许多多经典高效的算法,例如miller-rabin判断素数算法, ...
- 济南学习D3T1__线性筛和阶乘质因数分解
[问题描述] 从1− N中找一些数乘起来使得答案是一个完全平方数,求这个完全平方数最大可能是多少. [输入格式] 第一行一个数字N. [输出格式] 一行,一个整数代表答案对100000007取模之后的 ...
随机推荐
- C#设计模式:策略者模式(Stragety Pattern)
一,什么是策略模式? 1,针对同一命令或行为,不同的策略做不同的动作. 2,比如针对一组算法,将每个算法封装到具有公共接口的独立的类中,从而使它们可以相互替换.策略模式使得算法可以在不影响到客户端的情 ...
- Mac下安装nodejs,然后安装Vue-devtools工具
一.安装nodejs 1.这一步简单,只要上官网下载下来,直接按照提示安装就可以,mac版本的安装方法很简单. 下载nodejs的官方网址是: nodejs.org ,浏览器输入就可以跳转到了 ...
- JVM(1)之 JAVA栈
开发十年,就只剩下这套架构体系了! >>> 若想使自己编写的Java程序高效运行,以及进行正确.高效的异常诊断,JVM是不得不谈的一个话题.本"JVM进阶"专 ...
- hadoop工作流调度系统
常见工作流调度系统 Oozie, Azkaban, Cascading, Hamake 各种调度工具特性对比 特性 Hamake Oozie Azkaban Cascading 工作流描述语言 XML ...
- JS-01 书写规范
此部分内容整理自私教指导和自我体会:(持续更新...) 1.运算符左右两边留空格 (webstorm快捷键ctrl+alt+l): 2.判断值是否相等尽量用“===” 严格等于 : 3.编程中,可有可 ...
- html中内联元素和块级元素的区别
1.下表列出了内联元素和块级元素的主要区别 html中内联元素和块级元素的区别 块级元素 行内元素 独占一行,默认情况下,其宽度自动填满其父元素宽度 相邻的行内元素会排列在同一行里,直到一行排不下,才 ...
- Spring Boot 项目 Maven 配置
在配置基于Maven的Spring Boot项目的过程中,打包运行出现了一系列错误. 比如: mvn 中没有主清单属性.java.lang.NoClassDefFoundError: org/spri ...
- windows安装 阿里云的Fun工具
由于项目使用到了 函数计算,特此了解到了需要安装 阿里云的Fun工具 Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算.API 网关.日志服务等资源.它通过一个 ...
- Git--01 基础 - 远程仓库的使用
目录 Git 基础 - 远程仓库的使用 远程仓库的使用 查看远程仓库 添加远程仓库 从远程仓库中抓取与拉取 推送到远程仓库 查看某个远程仓库 远程仓库的移除与重命名 Git 基础 - 远程仓库的使用 ...
- go语言从例子开始之Example33.工作池
在这个例子中,我们将看到如何使用 Go 协程和通道实现一个工作池 . Example: package main import "fmt" import "time&qu ...