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,求最小的x,使得a+c*x==b%(2^k)

即c*x+(2^k)*y==(b-a+2^k)%(2^k)

直接上exgcd,然后调一调x得到最小解即可

 #include<cstdio>
#include<iostream>
#include<cstring>
#define LL long long
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL A,B,C,D,k;
inline LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(!b){x=;y=;return a;}
LL gcd=exgcd(b,a%b,x,y);
LL t=x;x=y;y=t-a/b*y;
return gcd;
}
inline LL calc(LL a,LL b,LL c)//Ax==B(mod C)
{
LL x,y;
LL tt=exgcd(a,c,x,y);
if (b%tt!=)return -;
x=x*b/tt;
LL ss=c/tt;
x=(x%ss+ss)%ss;
return x;
}
int main()
{
while (~scanf("%lld%lld%lld%lld",&A,&B,&C,&k)&&(A+B+C+k))
{
D=1ll<<k;
if (B==A){puts("");continue;}
B=(B-A+D)%D;
LL ans=calc(C,B,D);
if (ans==-)puts("FOREVER");
else printf("%lld\n",ans);
}
}

poj 2115

[暑假集训--数论]poj2115 C Looooops的更多相关文章

  1. [暑假集训--数论]hdu2136 Largest prime factor

    Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...

  2. [暑假集训--数论]hdu1019 Least Common Multiple

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  3. [暑假集训--数论]poj1365 Prime Land

    Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...

  4. [暑假集训--数论]poj2034 Anti-prime Sequences

    Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement ...

  5. [暑假集训--数论]poj1595 Prime Cuts

    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...

  6. [暑假集训--数论]poj2262 Goldbach's Conjecture

    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...

  7. [暑假集训--数论]poj2909 Goldbach's Conjecture

    For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...

  8. [暑假集训--数论]poj3518 Prime Gap

    The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...

  9. [暑假集训--数论]poj1730 Perfect Pth Powers

    We say that x is a perfect square if, for some integer b, x = b 2. Similarly, x is a perfect cube if ...

随机推荐

  1. 散列表的ASL计算

    题目: 已知关键字序列为{30,25,72,38,8,17,59},设散列表表长为15.散列函数是H(key)=key MOD 13,处理冲突的方法为二次探测法Hi= ( H(key) + di )m ...

  2. 洛谷P1481 魔族密码(LIS)

    题意 题目链接 给出一堆字符串,若一个串是另一个串的前缀 ,那么它们可以连接在一起 问最大的链接长度 Sol LIS沙比提其实是做完了才看出是LIS #include<cstdio> #i ...

  3. 【前端_js】js中数字字符串之间的比较

    js中字符串间的比较是按照位次优先,比较各字符的ASCII大小,包括数字字符串之间的比较. 1.console.log("1"<"3");//true 2 ...

  4. read指令使用方法

    read命令用于从标准输入中读取输入单行,并将读取的单行根据IFS变量分裂成多个字段,并将分割后的字段分别赋值给指定的变量列表var_name.第一个字段分配给第一个变量var_name1,第二个字段 ...

  5. linux关于权限

    用户权限:drwxr-x---. 8 root root 4096 8月 6 23:18 mnt 第一个root:所有者 即root用户第二个root:所有者所在的组mnt:所有者创建的文件夹Rwx: ...

  6. webpack+thymeleaf实现数据直出

    webpack动态插入thymeleaf模板,MVC将要使用数据传递到模板中渲染,得到的html就已经带有要初始显示的数据了github:https://github.com/947133297/we ...

  7. tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加

    tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加 大家都知道,在使用tp5的paginate获取分页数据之后,得到的是一个数据对象,但有时会碰到要对数据对象进行二次加工的情况, ...

  8. SoapUI(一)之webservice测试

    webservice测试需要具备的条件: 1.了解业务需求:如从客户端发送一个post请求给服务器,服务器将响应传给客户端. 2.需要一个明确的wsdl地址: 如天气预报的接口链接:http://ww ...

  9. 用 Tensorflow 建立 CNN

    稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记. 还有 google 在 udacity 上的 CNN 教程. CNN(Convolutional Neural Networks) 卷积神经网络简单 ...

  10. SSH无密码登录及远程拷贝命令SCP的使用

    SSH无密码登录 1.生成密钥对(公钥和私钥) $ cd /home/cen/.ssh $ ssh-keygen -t rsa #生成密钥,使用rsa方式进行加密,四个回车 $ ssh-copy-id ...