hdu 5019

 #include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <list>
#include <cassert>
#include <complex>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
//#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define TWO(x) (1<<(x))
#define TWOL(x) (1ll<<(x))
#define clr(a) memset(a,0,sizeof(a))
#define POSIN(x,y) (0<=(x)&&(x)<n&&0<=(y)&&(y)<m)
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long ll;
typedef long double LD;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<ll> VL;
typedef vector<PII> VPII;
typedef complex<double> CD;
const int inf=0x20202020;
const ll mod=;
const double eps=1e-; ll powmod(ll a,ll b) //return (a*b)%mod
{ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll powmod(ll a,ll b,ll mod) //return (a*b)%mod
{ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) //return gcd(a,b)
{ return b?gcd(b,a%b):a;}
// head namespace Factor {
const int N=;
ll C,fac[],n,mut,a[];
int T,cnt,i,l,prime[N],p[N],psize,_cnt;
ll _e[],_pr[];
vector<ll> d; inline ll mul(ll a,ll b,ll p) { //return (a*b)%p
if (p<=) return a*b%p;
else if (p<=1000000000000ll) return (((a*(b>>)%p)<<)+(a*(b&((<<)-))))%p;
else {
ll d=(ll)floor(a*(long double)b/p+0.5);
ll ret=(a*b-d*p)%p;
if (ret<) ret+=p;
return ret;
}
} void prime_table(){ //prime[1..tot]: prime[i]=ith prime
int i,j,tot,t1;
for (i=;i<=psize;i++) p[i]=i;
for (i=,tot=;i<=psize;i++){
if (p[i]==i) prime[++tot]=i;
for (j=;j<=tot && (t1=prime[j]*i)<=psize;j++){
p[t1]=prime[j];
if (i%prime[j]==) break;
}
}
} void init(int ps) { //initial
psize=ps;
prime_table();
} ll powl(ll a,ll n,ll p) { //return (a^n)%p
ll ans=;
for (;n;n>>=) {
if (n&) ans=mul(ans,a,p);
a=mul(a,a,p);
}
return ans;
} bool witness(ll a,ll n) {
int t=;
ll u=n-;
for (;~u&;u>>=) t++;
ll x=powl(a,u,n),_x=;
for (;t;t--) {
_x=mul(x,x,n);
if (_x== && x!= && x!=n-) return ;
x=_x;
}
return _x!=;
} bool miller(ll n) {
if (n<) return ;
if (n<=psize) return p[n]==n;
if (~n&) return ;
for (int j=;j<=;j++) if (witness(rand()%(n-)+,n)) return ;
return ;
} ll gcd(ll a,ll b) {
ll ret=;
while (a!=) {
if ((~a&) && (~b&)) ret<<=,a>>=,b>>=;
else if (~a&) a>>=; else if (~b&) b>>=;
else {
if (a<b) swap(a,b);
a-=b;
}
}
return ret*b;
} ll rho(ll n) {
for (;;) {
ll X=rand()%n,Y,Z,T=,*lY=a,*lX=lY;
int tmp=;
C=rand()%+;
X=mul(X,X,n)+C;*(lY++)=X;lX++;
Y=mul(X,X,n)+C;*(lY++)=Y;
for(;X!=Y;) {
ll t=X-Y+n;
Z=mul(T,t,n);
if(Z==) return gcd(T,n);
tmp--;
if (tmp==) {
tmp=;
Z=gcd(Z,n);
if (Z!= && Z!=n) return Z;
}
T=Z;
Y=*(lY++)=mul(Y,Y,n)+C;
Y=*(lY++)=mul(Y,Y,n)+C;
X=*(lX++);
}
}
} void _factor(ll n) {
for (int i=;i<cnt;i++) {
if (n%fac[i]==) n/=fac[i],fac[cnt++]=fac[i];}
if (n<=psize) {
for (;n!=;n/=p[n]) fac[cnt++]=p[n];
return;
}
if (miller(n)) fac[cnt++]=n;
else {
ll x=rho(n);
_factor(x);_factor(n/x);
}
} void dfs(ll x,int dep) {
if (dep==_cnt) d.push_back(x);
else {
dfs(x,dep+);
for (int i=;i<=_e[dep];i++) dfs(x*=_pr[dep],dep+);
}
} void norm() {
sort(fac,fac+cnt);
_cnt=;
rep(i,,cnt) if (i==||fac[i]!=fac[i-]) _pr[_cnt]=fac[i],_e[_cnt++]=;
else _e[_cnt-]++;
} vector<ll> getd() {
d.clear();
dfs(,);
return d;
} vector<ll> factor(ll n) { //return all factors of n cnt:the number of factors
cnt=;
_factor(n);
norm();
return getd();
} vector<PLL> factorG(ll n) {
cnt=;
_factor(n);
norm();
vector<PLL> d;
rep(i,,_cnt) d.push_back(make_pair(_pr[i],_e[i]));
return d;
} bool is_primitive(ll a,ll p) {
//assert(miller(p));
vector<PLL> D=factorG(p-);
rep(i,,SZ(D)) if (powmod(a,(p-)/D[i].first,p)==) return ;
return ;
}
} ll x,y,k;
int _;
int main() {
Factor::init();
for (scanf("%d",&_);_;_--) {
scanf("%I64d%I64d%I64d",&x,&y,&k);
vector<ll> c=Factor::factor(gcd(x,y)); //c:all factors of gcd(x,y)
sort(all(c)); // =all common factors of x and y
reverse(all(c));
if (SZ(c)<k) puts("-1"); else printf("%I64d\n",c[k-]);
}
}

xudyh的gcd模板的更多相关文章

  1. gcd模板(欧几里得与扩展欧几里得、拓展欧几里得求逆元)

    gcd(欧几里得算法辗转相除法): gcd ( a , b )= d : 即 d = gcd ( a , b ) = gcd ( b , a mod b ):以此式进行递归即可. 之前一直愚蠢地以为辗 ...

  2. gcd 模板

    声明 给 x,y 两个数,求 x,y 的最大公因数. 辗转相除法,直接套!!! function gcd(x,y:longint):longint; begin then exit(x) else e ...

  3. 最大公约数(gcd模板)

    int gcd(int a,int b) { ) { int t=a%b; a=b; b=t; } return a; }

  4. 最大公因数数gcd模板

    首先蒟蒻是在大佬的博客里学习的代码,代码风格多有相似之处,大佬博客https://www.cnblogs.com/lMonster81/p/10433902.html 最大公因数那,顾名思义就是两个数 ...

  5. 模板—扩展GCD*2

    有必要重新学一下扩展GCD emmmm. 主要是扩展GCD求解线性同余方程$ax≡b (mod p)$. 1.方程有解的充分必要条件:b%gcd(a,p)=0. 证明: $ax-py=b$ 由于求解整 ...

  6. poj 1061 青蛙的约会(扩展gcd)

    题目链接 题意:两只青蛙从数轴正方向跑,给出各自所在位置, 和数轴长度,和各自一次跳跃的步数,问最少多少步能相遇. 分析:(x+m*t) - (y+n*t) = p * L;(t是跳的次数,L是a青蛙 ...

  7. kgcd ,fmod,fgcd

    参考:NENU CS ACM模板made by tiankonguse  2.13 GCD 快速gcd: 位操作没学,真心不懂二进制,还是得学啊 code: int kgcd(){ if(!a || ...

  8. HDU 1576 (乘法逆元)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1576 题目大意:求(A/B)mod 9973.但是给出的A是mod形式n,n=A%9973. 解题思 ...

  9. ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 The Book List

    描述 The history of Peking University Library is as long as the history of Peking University. It was b ...

随机推荐

  1. Wcf:可配置的服务调用方式

    添加wcf服务引用时,vs.net本来就会帮我们在app.config/web.config里生成各种配置,这没啥好研究的,但本文谈到的配置并不是这个.先看下面的图: 通常,如果采用.NET的WCF技 ...

  2. Silverlight:针式打印机文字模糊的改善办法

    SL的打印功能,如果使用针式打印机,打出来的字很模糊,网上有一些文章介绍应该使用"Arial,SimSun"(即:宋体),但实际测试的结果,宋体依然很模糊. 下面是各种字体的测试: ...

  3. cookie记住密码功能

    很多门户网站都提供了记住密码功能,虽然现在的浏览器都已经提供了相应的记住密码功能 效果就是你每次进入登录页面后就不需要再进行用户名和密码的输入: 记住密码功能基本都是使用cookie来进行实现的,因此 ...

  4. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  5. Theano2.1.14-基础知识之理解为了速度和正确性的内存别名

    来自:http://deeplearning.net/software/theano/tutorial/aliasing.html Understanding Memory Aliasing for ...

  6. JSON简介以及用法汇总

    什么是JSON? JavaScript 对象表示法(JavaScript Object Notation). JSON是一种轻量级的数据交换格式,某个JSON格式的文件内部譬如可以长成这样: { &q ...

  7. Windows下搭建PHP环境:Apache+PHP+MySQL

    本文简单记录一下Windows下搭建PHP环境的过程,一些细节可以参照本文参考资料,此文不再赘述 准备工作: Windows下手工搭建PHP环境需要先下载相应的软件,需要注意的是Apache与PHP的 ...

  8. Android闹钟设置的解决方案

    Android设置闹钟并不像IOS那样这么简单,做过Android设置闹钟的开发者都知道里面的坑有多深.下面记录一下,我解决Android闹钟设置的解决方案. 主要问题 API19开始AlarmMan ...

  9. MATLAB读取和写入Excel文件

    1. 读取Excel文件 (a) 命令方式xlsread:  读取命令:[data,text]  = xlsread(FileName, SheetName, Range); data保存的是数据单元 ...

  10. bindService初步了解

    bindService的使用: 当需要调Service里面的方法时,可以用bindService() 首先定义一个类继承于Service,然后配置Manifest.xml文件 public class ...