HHU LCM and Walk (DFS+数论)

来源:互联网 发布:在淘宝网上买汽车配件 编辑:程序博客网 时间:2024/04/29 20:59

题目描述

Jerry has just learned some number theory, and can't wait to show his ability to Tom.

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

To show Tom his talents in math, he uses a special way of walk. If currently Jerry 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 walk 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 Jerry the number of possible starting grids that can reach (ex,ey)!

输入

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.

输出

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.

样例输入

36 106 82 8

样例输出

Case #1: 1Case #2: 2Case #3: 3
总结:
一开始的思路是如果终点是(ex,ey)的话,那么我们假设来的状态是(sx,ey)或者(ex,sy)然后就用隐式图搜索就可以结局,但是如果采取sx从ex-ey来搜的话会超时,我们可以对公式变形,得到sx=ex/(1+ey/gcd(ey,sx))这样的话我们只要枚举gcd就可以,这样之所以效率比上面的方法搞原因是我们枚举的只是ey的因子,而不用去搜索许多额外的值,这里搜索ey的因子的时候采用的这种方法是从1搜到sqrt(ey),那么如果i是因子,那么ey/i也是因子,但是还有一个特殊情况,当ey是平方数的时候,我们就会找到两个相同的因子,这里我们只能去搜一次,记得特判一下
#include <iostream>#include <cstring>#include <cstdio>#include <map>#include <cmath>using namespace std;typedef long long ll;const int maxn=100000;ll counter,cnt,ans;//struct state//{//ll x;//ll y;//ll hash;//bool operator <(const state &b) const//{//if(b.hash!=hash) return hash<b.hash;//if(b.x!=x) return x<b.x;//if(b.y!=y) return y<b.y;//}//}states[maxn];//map <state,int> vis;//ll gcd (ll a,ll b)//{//ll r=a%b;//if(r==0) return b;//else return gcd (b,r);//}//ll lcm(ll a,ll b)//{//return a/gcd(a,b)*b;//}//int  legal1(ll ex,ll sy,ll ey)//{//if(sy+lcm(ex,sy)!=ey) return 0;//cnt++;//states[cnt].x=ex;//states[cnt].y=sy;//states[cnt].hash=states[cnt].x*10+states[cnt].y;//if(!vis[states[cnt]]) //{//vis[states[cnt]]++;//return 1;//}//else //{//cnt--;//return 0;//}//}//int  legal2(ll sx,ll ey,ll ex)//{//if(sx+lcm(sx,ey)!=ex) return 0;//cnt++;//states[cnt].x=sx;//states[cnt].y=ey;//states[cnt].hash=states[cnt].x*10+states[cnt].y;//if(!vis[states[cnt]]) //{//vis[states[cnt]]++;//return 1;//}//else //{//cnt--;//return 0;//} //}//void dfs(ll ex,ll ey)//{//for(int i=1;i<=ey-ex;i++)//{//cout<<"("<<ex<<","<<i<<")"<<endl;//if(legal1(ex,i,ey))//{//cout<<"1"<<endl;//counter++;//dfs(ex,i);//}//}//for(int i=1;i<=ex-ey;i++)//{//cout<<"("<<i<<","<<ey<<")"<<endl;//if(legal2(i,ey,ex))//{//cout<<"1"<<endl;//counter++;//dfs(ex,i);//}//}//}void dfs(int x, int y){    ans++;    if(x < y) swap(x,y);    int p = sqrt(y - 0.5);    int i;    for(i = 1; i <= p; ++i)    {        if(y % i == 0)        {            if(x%(1+y/i) == 0&&gcd(x/(1+y/i),y) == i) dfs(x/(1+y/i),y);            if(x%(1+i) == 0&&gcd(x/(1+i),y) == y/i) dfs(x/(1+i),y);        }    }    if(i*i == y)    {        if(x%(1+i) == 0&&gcd(x/(1+i),y) == i) dfs(x/(1+i),y);    }}int main(){//freopen("input.txt","r",stdin);int T;scanf("%d",&T);for(int t=1;t<=T;t++){ll ex,ey;ans=0;scanf("%lld%lld",&ex,&ey);vis.clear();counter=1;cnt=0;dfs(ex,ey);printf("Case #%d: %lld\n",t,ans);}return 0;}

 
0 0
原创粉丝点击