C Looooops(扩展欧几里得求模线性方程)

79 篇文章 0 订阅
73 篇文章 0 订阅

Link:http://poj.org/problem?id=2115


C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20167 Accepted: 5416

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

Source



AC code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#define PI acos(-1.0)
#define LINF 1000000000000000000LL
#define eps 1e-8
#define LL long long
#define MAXN 100010 
#define MOD 1000000007
using namespace std;
const int INF=0x3f3f3f3f;
LL exgcd(LL A,LL &x,LL B,LL &y)
{
    LL x1,y1,x0,y0;
    x0=1;y0=0;
    x1=0;y1=1;
    LL r=(A%B+B)%B;
    LL q=(A-r)/B;
    x=0;y=1;
    while(r)
    {
        x=x0-q*x1;
        y=y0-q*y1;
        x0=x1;
        y0=y1;
        x1=x;y1=y;
        A=B;B=r;r=A%B;
        q=(A-r)/B;
    }
    return B;
}
LL ABS(LL x)
{
	return x>=0?x:-x;
}

//求ax=b(mod n)等价于ax+ny=b,故可以这样转化过来 
int main()
{
    long long A,B,C,k,x,y; 
    while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)!=EOF)  
    {
    	if(!A&&!B&&!C&&!k)break; 
    	long long a,b,n,d; 
    	a=C;
    	b=B-A; 
        n=(long long)1<<k;
		d=exgcd(a,x,n,y); //求a,n的最大公约数d=gcd(a,n)和方程d=ax+by的系数x、y   
		if(b%d)printf("FOREVER\n"); 
		else  
		{ 
			x=(x*(b/d))%n;//方程ax=b(mod n)的最小解  
			x=(x%(n/d)+n/d)%(n/d); //方程ax=b(mod n)的最整数小解  
			printf("%I64d\n",x);  
		} 
		/* 不知为何,下面的输出会RE!!! 
        /*LL x0,y0,flag=0;
        if(a<b){flag=a;a=b;b=flag;flag=1;}//默认令a>=b,flag=1说明有交换,则对应的系数x和y最后输出时也要交换后输出 
        LL d=exgcd(a,x0,b,y0);//返回a、b的最大公约数 
        //求ax+by=c 
        if(c%d!=0)//无解 
        {
        	printf("FOREVER\n");
		}
		else
		{
			x0=x0*(c/d);//x的一个解
	        y0=y0*(c/d);//y的一个解
	        LL t=y0/(a/d),minx=LINF,miny=LINF,minn=LINF;
	        for(int i=t-1;i<=t+1;i++)
	        {
	        	//x的所有解是x=x0+b/d*t,y的所有解是:y=y-a/d*t。
	            x=x0+b/d*i;
	            y=y0-a/d*i;
	            //printf("%d %d\n",x,y);//打印所有解 
	           /* if(ABS(x)+ABS(y)<minn)
	            {
	                minn=ABS(x)+ABS(y);
	                minx=ABS(x);
	                miny=ABS(y);
	            }*/
	           /* if(x>=0)
	            {
	            	minx=min(minx,x);
				}
	            if(y>=0)
	            {
	            	miny=min(miny,y);
				}
	        }
	        if(flag)printf("%lld\n",miny);
	        else printf("%lld\n",minx);
	       /* if(flag)printf("%lld\n",miny);
	        else printf("%lld\n",minx);*/
	        //printf("%lld\n",minx);  
		//}
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值