LCM Walk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 47    Accepted Submission(s): 31

Problem Description
A frog has just learned some number theory, and can't wait to show his ability to his girlfriend.

Now the frog is sitting on a grid map of infinite rows and columns. Rows are numbered 1,2,⋯ from the bottom, so are the columns. At first the frog is sitting at grid (sx,sy), and begins his journey.

To show his girlfriend his talents in math, he uses a special way of jump. If currently the frog is at the grid (x,y), first of all, he will find the minimum z that can be divided by both x and y, and jump exactly z steps to the up, or to the right. So the next possible grid will be (x+z,y), or (x,y+z).

After a finite number of steps (perhaps zero), he finally finishes at grid (ex,ey). However, he is too tired and he forgets the position of his starting grid!

It will be too stupid to check each grid one by one, so please tell the frog the number of possible starting grids that can reach (ex,ey)!

 
Input
First line contains an integer T, which indicates the number of test cases.

Every test case contains two integers ex and ey, which is the destination grid.

⋅ 1≤T≤1000.
⋅ 1≤ex,ey≤109.

 
Output
For every test case, you should output "Case #x: y", where x indicates the case number and counts from 1 and y is the number of possible starting grids.
 
Sample Input
3
6 10
6 8
2 8
 
Sample Output
Case #1: 1
Case #2: 2
Case #3: 3
 
Source
 
 

2015acm上海区域赛的第三道水题。。第一开始以为是推公式然后o(1)求出答案,然而貌似并不能,最后还是想了个暴力枚举公因子吧。。

容易得知,x,y里面肯定是较小的数不变,较大的那个数是从之前某个数变化来的,假设x>y,(x,y)是从(x1,y)变化来的,那么:

x = x1 + x1*y/gcd(x1,y);则x1 = x/(1 + y/gcd(x1,y));

那么就很好说了,枚举gcd(x1,y),即枚举y的因子,反求出x1,然后判断x1是否合理,合理的话就继续递归(x1,y),这里枚举因子有一个细节需要

注意,就是对于y是完全平方数的时候,枚举上界是sqrt(y-0.5),然后对于x = sqrt(y)的情况特判,因为忘了注意这点此贡献了一次WA。。

为什么要这样子呢。。因为O(根号n)枚举因子时,如果i是y的因子,那么y/i也是y的因子,这里要判断两个因子,但是i*i=y时,必须只判断一次

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std; int t;
int x,y;
int ans; int gcd(int x, int y)
{
return x == ?y : gcd(y%x,x);
} void dfs(int x, int y)
{
ans++;
if(x < y) swap(x,y);
int p = sqrt(y - 0.5);
int i;
for(i = ; i <= p; ++i)
{
if(y % i == )
{
if(x%(+y/i) == &&gcd(x/(+y/i),y) == i) dfs(x/(+y/i),y);
if(x%(+i) == &&gcd(x/(+i),y) == y/i) dfs(x/(+i),y);
}
}
if(i*i == y)
{
if(x%(+i) == &&gcd(x/(+i),y) == i) dfs(x/(+i),y);
}
} int main()
{
int cas = ;
for(cin >> t; cas <= t; ++cas)
{
ans = ;
scanf("%d%d",&x,&y);
dfs(x,y);
printf("Case #%d: %d\n",cas,ans);
}
}

HDU5584 LCM Walk 数论的更多相关文章

  1. hdu-5584 LCM Walk(数论)

    题目链接:LCM Walk Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)To ...

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

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

  3. HDU 5584 LCM Walk 数学

    LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...

  4. [HDOJ5584]LCM Walk(数论,规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 给一个坐标(ex, ey),问是由哪几个点走过来的.走的规则是x或者y加上他们的最小公倍数lcm ...

  5. HDU - 5584 LCM Walk (数论 GCD)

    A frog has just learned some number theory, and can't wait to show his ability to his girlfriend. No ...

  6. LCM Walk HDU - 5584

    A frog has just learned some number theory, and can't wait to show his ability to his girlfriend. No ...

  7. hdu 5584 LCM Walk(数学推导公式,规律)

    Problem Description A frog has just learned some number theory, and can't wait to show his ability t ...

  8. HDU 5584 LCM Walk(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意:(x, y)经过一次操作可以变成(x+z, y)或(x, y+z)现在给你个点(ex, e ...

  9. hdu 5584 LCM Walk

    没用运用好式子...想想其实很简单,首先应该分析,由于每次加一个LCM是大于等于其中任何一个数的,那么我LCM加在哪个数上面,那个数就是会变成大的,这样想,我们就知道,每个(x,y)对应就一种情况. ...

随机推荐

  1. 分布式内存对象缓存 memcached

    分布式内存对象缓存 许多Web 应用程序都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示.但随着数据量的增大,访问的集中,就会出现REBMS的负担加重,数据库响应恶化,网站显示延迟等 ...

  2. StoryBoard 设置TabBar SelectImage 和tintColor

    如图:StoryBoard 结构是 Tabbar + Navi + ViewController 需求:需要修改TabBar的Image 和SelectImage 设置Image 设置SelectIm ...

  3. StringBuffer和StringBuilder使用方法比較

    StringBuffer是字符串缓冲区,是一个容器. 特点: 1,长度是可变化的. 2,能够字节操作多个数据类型. 3,终于会通过toString方法变成字符串. C create U update ...

  4. 解决Android单个dex文件不能超过65536个方法问题

    当我们的项目代码过大时,编译运行时会报Unable to execute dex: method ID not in[0, 0xffff]: 65536)错误.当出现这个错误时说明你本身自己的工程代码 ...

  5. OD: Exploit Me - Overwrite Return Address

    修改邻接变量的方法对代码环境限制比较多,更通用.更强大的方法是修改 EBP.返回地址等状态值. 为了方便调试,修改之前的代码如下: #include<stdio.h> #include&l ...

  6. github 预览html

    在网址前加 http://htmlpreview.github.io/?

  7. Swift 字符串连接

    // 使用+直接相加 var i = var str = "Hello" str = str + " jinpangpang" // 可以使用这种方式连接整值 ...

  8. C#--对象的相等比较

    对象相等比较机制对于引用类型的变量和值类型的变量来说是不同的,下面分别介绍引用类型和值类型的相等比较. 首先来看System.Object的部分定义: public class Object { // ...

  9. 关于R文件丢失的一个问题

    android studio在编辑布局文件时,一般为了省事,如TextView控件中的text属性这样写 android:text="<500",编译不会报错,但是运行时会出 ...

  10. MyEclipse笔记(2):debug的使用

    对于程序代码而言,学会调debug是重中之重,依此,掌握该技巧 以算1到50的和的代码为例: package com.front.action; public class debug { public ...