poj 2115 C Looooops(模线性方程)

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


题意:对于C的循环(for i = A; i != B; i+=C)问在k位存储系统内循环多少次结束;

   若循环有限次能结束输出次数,否则输出 FOREVER;

解:设x为循环次数;

   (A+C*x)%2^k = B;  

  则 C*x+A = 2^k*y+B; 

  所以 C*x - 2^k*y = B-A; 类似于a*x+b*y = c (或 a*x = c(mod b))模线性方程的形式,根据扩展欧几里得算法解决,模板题。


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#define LL long long
#define _LL __int64
#define eps 1e-8

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 10;

/*
(A+C*x)%2^k = B;
那么 A + C*x = 2^k * y + B;
C*x - 2^k*y = B-A;
*/

LL extend_gcd(LL a, LL b, LL &x, LL &y)
{
	if(b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	LL d = extend_gcd(b,a%b,x,y);
	LL t = x;
	x = y;
	y = t - a/b*y;
	return d;
}

int main()
{
	int A,B,C,k;
	LL a,b,c;
	LL x,y;

	while(~scanf("%d %d %d %d",&A,&B,&C,&k))
	{
		if(A == 0 && B == 0 && C == 0 && k == 0)
			break;
		a = C;
		b = 1LL<<k;
		c = B-A;

		LL d = extend_gcd(a,b,x,y);
		if(c%d != 0)
		{
			printf("FOREVER\n");
			continue;
		}
		x = x * (c/d);
		x = (x%(b/d) + (b/d)) % (b/d);
		printf("%lld\n",x);

	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值