hdu 2669 C - Romantic

题意: a*x+ by= 1; 给出a,b;求x,y;  (0<a,b<2^31)

  

这道题摆明的扩展GCD,但是我写了很多很多次都没写对,最后发现是求出X',Y'(ax'+by'= gcd(a,b));  然后求最小非负整数解的时候出现了问题

设 at+bp= gcd(a,b);

通过这题我证明了

ans1= t;
  ans1= (ans1%b + b)%b;// b为方程的循环节

这种方法求最小非负整数是正确的(不用枚举,尽管这题枚举也能过)

注意定义的时候用__int64, 不然会wa的

代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<limits.h>
using namespace std;
__int64 t,p;
__int64 gcd(__int64 a,__int64 b)
{
	return b==0? a: gcd(b,a%b);
}
void exgcd(__int64 a,__int64 b)
{
	if(b==0)
	{
		t= 1;
		p= 0;
	}
	else
	{
		exgcd(b,a%b);
		int temp= t;
		t= p;
		p= temp- a/b*p;
	}
}
int main()
{
	__int64 a,b,c;
	while(scanf("%I64d%I64d",&a,&b)!=EOF)
	{
		__int64 d= gcd(a,b);
		c= 1;
		if(d!=1)
		{
			printf("sorry\n");
		}
		else
		{
			exgcd(a,b);
			__int64 ans1,ans2;
			ans1= t;
			ans1= (ans1%b + b)%b;
			ans2= (1- ans1*a)/ b;
			printf("%I64d %I64d\n",ans1,ans2);
		}
	}
return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值