input

T  1<=T<=1000

x y

output

有多少个起点可以走n(n>=0)步走到(x,y),只能从(x,y)走到(x,y+lcm(x,y))/(x+lcm(x,y),y)

标准解:从(x,y0)走到(x,y),则设x=ag,y0=bg,g=gcd(x,y0),有y=bg+abg=(a+1)bg,因为a,b互质,a,(a+1)互质,所以a和(a+1)b互质,所以若可以从(x,y0)走到(x,y),有gcd(x,y0)=gcd(x,y),然后将x和y中gcd(x,y)除去之后不断除以(x+1)即可

 #include <iostream>
#include <cstdio>
#include <set>
#include <algorithm> using namespace std; typedef long long LL; //求最大公约数
LL gcd(LL a, LL b) { if(!b) return a; else return gcd(b,a%b); } int main()
{
int t,Case = ;
scanf("%d",&t);
while(t--)
{
LL ex,ey; //终点坐标 scanf("%lld%lld",&ex,&ey);
LL GCD = gcd(ex,ey);
ex/=GCD,ey /=GCD;
int ans = ;
while()
{
if(ey < ex) swap(ex,ey);
ans++;
if(ey % (ex+)) break;
ey /= (ex+);
} printf("Case #%d: %d\n",++Case,ans);
}
}

answer

 #include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#define MAX 100000
#define LL long long
int cas=,T,x,y,d[],dn;
void find(int x,int *d,int& dn)
{
dn=-;
int m=sqrt(x);
for(int i=;i<=m;i++) if(x%i==) d[++dn]=i;
for(int i=dn;i>=;i--) d[++dn]=x/d[i];
}
int gcd(int a,int b) { return b==?a:gcd(b,a%b); }
int main()
{
// freopen("in","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&x,&y);
int step=,flag=;
while(flag&&x!=y)
{
flag=;
if(x>y) std::swap(x,y);
int g=gcd(x,y);
find(g,d,dn);
for(int i=dn;i>=;i--)
{
if(y%(d[i]+x)==)
{
int y1=y/(d[i]+x)*d[i];
if(gcd(x,y1)==d[i]) { y=y1;step++;flag=;break; }
}
}
}
printf("Case #%d: %d\n",cas++,step);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
}

My Code

hdu 5584 gcd/lcm/数学公式的更多相关文章

  1. 2015多校第8场 HDU 5382 GCD?LCM! 数论公式推导

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5382 题意:函数lcm(a,b):求两整数a,b的最小公倍数:函数gcd(a,b):求两整数a,b的最 ...

  2. hdu 5382 GCD?LCM! - 莫比乌斯反演

    题目传送门 传送门I 传送门II 题目大意 设$F(n) = \sum_{i = 1}^{n}\sum_{j = 1}^{n}\left [ [i, j] + (i, j) \geqslant n \ ...

  3. hdu 5382 GCD?LCM!

    先考虑化简f函数 发现,f函数可以写成一个递归式,化简后可以先递推求出所有f函数的值, 所以可以先求出所有S函数的值,对于询问,O(1)回答 代码: //File Name: hdu5382.cpp ...

  4. L - LCM Walk HDU - 5584 (数论)

    题目链接: L - LCM Walk HDU - 5584 题目大意:首先是T组测试样例,然后给你x和y,这个指的是终点.然后问你有多少个起点能走到这个x和y.每一次走的规则是(m1,m2)到(m1+ ...

  5. 【HDU 5382】 GCD?LCM! (数论、积性函数)

    GCD?LCM! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  6. HDU 4497 GCD and LCM(数论+容斥原理)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  7. HDU 4497 GCD and LCM 素因子分解+ gcd 和 lcm

    题意: 给两个数,lll 和 ggg,为x , y , z,的最小公倍数和最大公约数,求出x , y , z 的值有多少种可能性 思路: 将x , y , z进行素因子分解 素因子的幂次 x a1 a ...

  8. hdu-3071 Gcd & Lcm game---质因数分解+状态压缩+线段树

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3071 题目大意: 给定一个长度为n的序列m次操作,操作的种类一共有三种 查询 L :查询一个区间的所 ...

  9. Mathematics:GCD & LCM Inverse(POJ 2429)

    根据最大公约数和最小公倍数求原来的两个数 题目大意,不翻译了,就是上面链接的意思. 具体思路就是要根据数论来,设a和b的GCD(最大公约数)和LCM(最小公倍数),则a/GCD*b/GCD=LCM/G ...

随机推荐

  1. 366. Find Leaves of Binary Tree C#

    Example:Given binary tree 1 / \ 2 3 / \ 4 5 Returns [4, 5, 3], [2], [1]. Explanation: 1. Removing th ...

  2. mac版tomcat修改端口无法访问,80端口无法访问

    在mac上安装好了tomcat,修改了端口为80,没想到关闭tomcat时提示出错,而且无法访问,原来我犯了两个错误. 1.我用的是mac上的文本编辑.app打开然后修改的,重新修改为8080也不行, ...

  3. 向量空间(Vector Spaces)

    向量空间(Vector Spaces) 向量空间又称线性空间,是线性代数的中心内容和基本概念之一.在解析几何里引入向量的概念后,是许多问题的处理变得更为简洁和清晰,在此基础上的进一步抽象化,形成了与域 ...

  4. php入门 数据类型 运算符 语言结构语句 函数 类与面向对象

    php PHP-enabled web pages are treated just like regular HTML pages and you can create and edit them ...

  5. hive-1.2.1安装步骤

    一.Hive安装和配置 1.先决条件 已经安装好hadoop-2.4.1,hbase-1.0.0. 2.下载Hive安装包 当前Hive可到apache官网下载,选择的是hive-1.2.1.运行: ...

  6. android源码查看所有分支切换分支

    cd .repo/manifests git branch -a repo init -b android-4.1.2_r1 repo sync

  7. HDU 3362 Fix(状压dp)

    Fix Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  8. final使用

    final修饰  基本数据类型时候   对应的 数据不能改变:::final修饰 对象类型 ,那么对应的引用地址不能改变(对象中的值可以改变): 如果final修改方法,那么该方法不能被子类重写 :: ...

  9. openstack私有云布署实践【2 安装前的服务器基本环境准备】

    服务器物理机都安装centos7.2 1511版本 , 此次采用的分区方式全是自动XFS格式LVM,在装系统时就将所有本地raid5硬盘都加入LVM全用了.默认/home目录有着最大的硬盘空间 并且我 ...

  10. jquery设置select选中

    /*设置select选中开始*/ var prod_type=$('.prod_type').val(); //alert(prod_type); var select = document.getE ...