HHU LCM and Walk (DFS+数论)

本文介绍了一种特殊的路径寻迹算法,该算法用于解决Jerry在无限网格地图中寻找从起点到终点(ex, ey)的所有可能路径的问题。通过数学推导简化了原始问题,并提出了一种高效的枚举因子的方法,避免了不必要的搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

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 first Jerry 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≤10 9.

输出

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.

样例输入

3
6 10
6 8
2 8

样例输出

Case #1: 1
Case #2: 2
Case #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;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值