POJ 1061 青蛙的约会

本文提供了一种解决POJ 1061问题的有效算法,该问题涉及求解特定线性方程组中变量t的最小值。通过使用辗转相除法求最大公约数并结合代数技巧,文章详细展示了如何找到满足条件的最小正整数解。

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

题目地址:http://poj.org/problem?id=1061

就是求(x-y)*t +(n-m)*k = -a 的式子中t的最小解,但注意k!=0

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
void gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
	if(!b) {d=a;x=1;y=0;}
	else{
		gcd(b,a%b,d,y,x);y-=x*(a/b);
	}
}
int main()
{
	LL x,y,m,n,L;
	while(cin>>x>>y>>m>>n>>L)
	{
		LL a=x-y,b=n-m,d;
		gcd(b,-L,d,x,y);
		if((-a)%d) {
			cout<<"Impossible"<<endl;
			continue;
		}
		LL s=(-L)/d,k=(-a)/d;
		LL ans=fabs((k*x-s)%s);
		if((ans*b+a)%L==0) ans=fabs(L-ans); //处理k==0的情况 
		cout<<ans<<endl;
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值