HDU 5844 LCM Walk(数学逆推)

http://acm.hdu.edu.cn/showproblem.php?pid=5584

题意:

现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k)。现在给出终点坐标,求有多少个起点可以通过这种变化方式得到终点。

思路:

现在假设我们处于(x,y)这个坐标上,x和y的最大公约数为k,x和y用k来表示的话可以表示为x=$m_{1}$,y=$m_{2}$。

那么接下来可以得到($m_{1}$k,$m_{2}$k+$m_{1}$$m_{2}$k)或者 ($m_{1}$$m_{2}$k,$m_{2}$k)。(这里$m_{1}$$m_{2}$k就是x和y的最小公倍数)

可以看到坐标变化前后它们的最大公约数都是相同的,都是k。然后根据这两个坐标进行逆推即可,每次只需要保留小的那个坐标,这样就能保证gcd还能为k。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5
 6 int x, y;
 7
 8 int gcd(int a, int b)
 9 {
10     return b==0?a:gcd(b,a%b);
11 }
12
13 int main()
14 {
15     int T;
16     scanf("%d",&T);
17     int kase = 0;
18     while(T--)
19     {
20         scanf("%d%d",&x,&y);
21         int ans = 0;
22         if(x>y) swap(x,y);
23         int k = gcd(x,y);
24         while(y%(x+k)==0)
25         {
26             y=y/(x/k+1);
27             ans++;
28             if(x>y) swap(x,y);
29         }
30         printf("Case #%d: ",++kase);
31         printf("%d\n",ans+1);
32     }
33 }
时间: 2024-11-07 19:38:19

HDU 5844 LCM Walk(数学逆推)的相关文章

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

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

hdu 3853 LOOPS (概率dp 逆推求期望)

题目链接 LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 2630    Accepted Submission(s): 1081 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to h

HDU - 5584 LCM Walk (数论 GCD)

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 sit

HDU 1176-免费馅饼(DP_逆推)

免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26290    Accepted Submission(s): 8952 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的1

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5584 给一个坐标(ex, ey),问是由哪几个点走过来的.走的规则是x或者y加上他们的最小公倍数lcm(x, y). 考虑(ex, ey)是由其他点走过来的,不妨设当走到(x,y)时候,gcd(x, y)=k,x=k*m1, y=k*m2. 下一步有可能是(x, y+x*y/gcd(x, y))或者是(x+x*y/gcd(x,y), y). 用k和m1,m2来表示为(k*m1, k*m2+m1*m2

hdu 4823 Energy Conversion(数学)

题目链接:hdu 4823 Energy Conversion 题目大意:中文题,不解释. 解题思路:首先判断一下m是否已经大于n了,如果大于那么就是0,假设中间变换的各个值为ai,那么bi=ai+c,bi数组为等比数组(可推),所以就有了cnt=log((n+c)a)log(double(k)),结果为浮点数,需要向上取整. #include <cstdio> #include <cstring> #include <cmath> int main () { int

hdu 4710 Balls Rearrangement (数学思维)

题意:就是  把编号从0-n的小球对应放进i%a编号的盒子里,然后又买了新盒子, 现在总共有b个盒子,Bob想把球装进i%b编号的盒子里.求重置的最小花费. 每次移动的花费为y - x ,即移动前后盒子编号的差值的绝对值. 算法: 题目就是要求                  先判断  n与  lcm(a,b)的大小,每一个周期存在循环,这样把区间缩短避免重复计算. 如果n>lcm(a,b)则   ans = (n/lcm)*solve(lcm)+solve(n%lcm) 否则   ans =

HDU5584 LCM Walk 数论

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 hi

hdu 4869 Turn the pokers(递推&amp;组合数学&amp;逆元)

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1279    Accepted Submission(s): 466 Problem Description During summer vacation,Alice stay at home for a long time, with nothing t