欧几里得和拓展欧几里得

欧几里得算法又称辗转相除法,用于计算两个整数a和b的最大公约数

定理:gcd(a,b)= gcd(b,a mod b)

证明:

         a可以表示成a= kb + r,则r = a mod b

    1.假设d是a,b的一个公约数,则有 a|d,b|d,而r= a - kb,因此r|d

       因此d是(b,a mod b)的公约数,证明充分性

         2.假设d是(b,a mod b)的公约数,则b|d, r|d,但是a = kb +r

       因此d也是(a,b)的公约数,证明必要性

           因此(a,b)和(b,amod b)的公约数是一样的,其最大公约数也必然相等

           当b=0时,(a0)的最大公约数是a

         代码如下:

int gcd(int a, int b)
{
  if(b == 0)      
    return a;
    return gcd(b, a % b);
}

拓展欧几里得算法用于在已知a,b求解一组x,y使得a*x+b*y=God(a,b)(解一定存在,根据数论中的相关定理)。拓展欧几里得常用在模线性方程及方程组中

实现代码:

int exgcd(int a, int b, int &x, int &y){
  if(b == 0){
  x = 1;
  y = 0;
   return a;
  }
  int r = exgcd(b, a % b, x, y);
  int t = x;
  x = y;
  y = t - a / b * y;
   return r;
  }

       a'= b, b' = a % b 而言,我们求得 x, y使得 a'x+ b'y = gcd(a',b')

  由于b'= a % b = a - a / b * b(这里的/是程序设计语言中的除法)

  那么可以得到:

      a'x+ b'y = gcd(a',b')===>

      bx+ (a - a / b * b)y = gcd(a',b') = gcd(a,b)===>

      ay+b(x - a / b*y) = gcd(a,b)

      因此对于a和b而言,他们的相对应的p,q分别是y和(x-a/b*y).


我们来看几道例题:

一,poj-1061青蛙的约会

青蛙的约会
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 122308 Accepted: 25943

Description

两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。 
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。 

Input

输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。

Output

输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"

Sample Input

1 2 3 4 5

Sample Output

4

Source


代码实现如下:

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#define ll long long int
using namespace std;
long long exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    ll r=exgcd(b,a%b,y,x);
    y-=x*(a/b);
    return r;
}
int main()
{
    ll x,y,m,n,L;
    ll a,b,c,gcd;
    while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L)!=EOF)
    {
        a=m-n;
        b=L;
        c=y-x;
        if(a<0)
        {
            a=-a;
            c=-c;
        }
        gcd=exgcd(a,b,x,y);
        if(c%gcd!=0)
            printf("Impossible");
        else
        {
            x=x*c/gcd;
            int t=b/gcd;
            if(x>=0)
                x=x%t;
            else
                x=x%t+t;
            printf("%lld\n",x);
        }
    }
    return 0;
}

二, hdu-2669

                               Romantic

                                                Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                Total Submission(s): 8416    Accepted Submission(s): 3581
 
Problem Description
The Sky is Sprite.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You.
................................Write in English class by yifenfei



Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.
 

Input
The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)
 

Output
output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put "sorry" instead.
 

Sample Input
  
  
77 51 10 44 34 79
 

Sample Output
  
  
2 -3 sorry 7 -3
代码实现如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<iomanip>
#define ll long long int
using namespace std;
long long exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
	{
	   x=1;
	   y=0;
	   return a;	
	}	
	ll re=exgcd(b,a%b,y,x);
	y=y-x*(a/b);
	return re;
} 
int main()
{
	ll a,b,x,y;
	while(cin>>a>>b)
	{
		ll gcd=exgcd(a,b,x,y);
		if(gcd!=1)
		{
			cout<<"sorry"<<endl; 
		}
		else
		{
			while(x<0)
			{
				x=x+b;
				y=y-a;
			}
			cout<<x<<" "<<y<<endl; 
		}
	}
return 0;	
} 





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值