拓展欧几里德 (附 POJ 2115)

关于拓展欧几里德,别人已经写的很好的了,我也自己写一下,方便以后自己复习。

其最基础的思想就是 gcd(a,b)=gcd(b,a mod b),其中 gcd的意思是求最大公约数。。

拓展欧几里德是用来求 x,y 是的a*x+b*y=gcd(a,b),---我们这样想 对于a'=b, b'=a%b=a-(a/b*b),

 a*x+b*y=gcd(a,b)

a'*x0+b'*y0=gcd(a',b')=gcd(a,b)=a*x+b*y

b*x0+(a-a/b*b)y0=a*x+b*y

a*y0+b*(x0-a/b*y0)=a*x+b*y

所以:  x=y0   ; y=x0-a/b*y0;


所以  拓展欧几里德的函数为 

int extended_euclid(int s,int t,int &x,int &y)
{
	if(t==0)
	{
		x=1;y=0
	    return s;
	}
	else 
	{
		int i=extended_euclid(t,s%t);
		int temp=x;
		x=y;
		y=temp-(s/t*y);
	}
	return i;
}

用此思想求线性同余方程:a*x+b*y=n ; (有整数解的充要条件是 (n%(a,b)==0) )

我们可以先求出一组x0,y0 是的a*x0+b*y0=(a,b)

 然后 等号左右同时除以(a,b)  得:    a*x0/(a,b)+b*y0/(a,b)=1 

接着等号左右同时乘以 n 就得到   :    a*x0/(a,b)*n + b*y0/(a,b)*n=n

  所以   得到: x=x0/(a,b)*n     y=y0/(a,b)*n  为方程的一组解了。

   若 (a,b)=1  且 x0,y0,为方程 a*x+b*y=n的一组解  ,那么 x=x0+b*t ,y=y0-a*t (t为任意整数)都是方程的解。

而往往题目中要求求最小的解,那么我们就可以将一个特解xt=b/ab),x=(x%t+t%t就可以了。



poj 2115

 

C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10534 Accepted: 2475

Description

A Compiler Mystery: We are given a C-language style for loop of type 
for (variable = A; variable != B; variable += C)

  statement;

I.e., a loop which starts by setting variable to value A and while variable is not equal to B, repeats statement followed by increasing the variable by C. We want to know how many times does the statement get executed for particular values of A, B and C, assuming that all arithmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2 k) modulo 2 k

Input

The input consists of several instances. Each instance is described by a single line with four integers A, B, C, k separated by a single space. The integer k (1 <= k <= 32) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, C < 2 k) are the parameters of the loop. 

The input is finished by a line containing four zeros. 

Output

The output consists of several lines corresponding to the instances on the input. The i-th line contains either the number of executions of the statement in the i-th instance (a single integer number) or the word FOREVER if the loop does not terminate. 

Sample Input

3 3 2 16
3 7 2 16
7 3 2 16
3 4 2 16
0 0 0 0

Sample Output

0
2
32766
FOREVER

分析:题目给出 a,b,c,k,且1<=a,b,c<=2^k;  求最小的x ,使得 ( a+c*x ) mod 2^k = b。

喵呜的推导:               a+c*x=b mod 2^k   

                           即:   a+c*x=  b (mod 2^k)

                           即:   a+c*x-b |2^k

  所以存在t  使得 :     t*2^k =a+c*x-b;

                     所以:     t*2^k-c*x=a-b;   

   然后用 拓展欧几里德的思想做即可。。。

代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

__int64  n,x,y;

__int64 extended_euclid(__int64 s,__int64 t)  //拓展欧几里德算法
{
	__int64 i,temp;
    if(t==0)
    {
        x=1;
        y=0;
        return s;
    }
	else 
	{
      i=extended_euclid(t,s%t);
      temp=x;
      x=y;
      y=temp-(s/t*x);
	}
	return i;
}

int main()
{
    __int64 a,b,c,i,j,e,m,k,d;
    while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k))
    {
        if(a==0 && b==0 && c==0 && k==0)
           break;
        d=(__int64)1<<k;    //超过30位的要定义1为 64 位,看discuss里面才知道的。。。
        e=extended_euclid(c,d);
        if((b-a)%e) printf("FOREVER\n");
        else
        {
            a=x*(b-a)/e%d+d;           //公式推导。
            printf("%lld\n",a%(d/e));   //求最小的那个 要 mod (d/e)
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值