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. 对于无法激活的系统—使用rearm命令延长试用期

    1.首先安装后,有一个30天的使用期. 2.在30天试用期即将结束时,用rearm命令后重启电脑,剩余时间又回复到30天.微软官方文档中声明该命令只能重复使用3次,也说是说总共可以免费体验120天. ...

  2. el-upload控件一次接口请求上传多个文件

    el-upload组件默认情况下上传多少个文件就会请求多少次上传接口,如何一次上传多个文件而不必多次请求上传接口呢?直接看代码 html <el-upload :action="act ...

  3. Java 程序设计总复习题

    Java程序设计总复习题 1.编写一个Java程序在屏幕上输出“你好!”. //programme name Helloworld.java public class Helloworld { pub ...

  4. vscode wepy 用户自定义代码片段

    VSCode wepy 自定义代码片段 { "wepy-page": { "prefix": "wepy", "body" ...

  5. 服务端Latex解析成图片或者HTML或者SVG方案

    Latex公式表达式在服务端进行转换成可用数据 使用语言与扩展 node.js Mathjax (文档链接) MathJax在nodejs上解决方案 mathjax/MathJax-node(GitH ...

  6. MTCNN学习资源

    MTCNN pytorch版本的实现 TropComplique/mtcnn-pytorch https://github.com/TropComplique/mtcnn-pytorch MTCNN实 ...

  7. GNU中的关键字typeof

    如果你是 C++ 程序员,应该接触过 C++11 里的 decltype 操作符,它的作用是自动推导表达式的数据类型,以解决泛型编程中有些类型由模板参数决定而难以(甚至不可能)表示的问题.其实这个特性 ...

  8. 动态规划:HDU-1203-0-1背包问题:I NEED A OFFER!

    解题心得: 动态规划就是找到状态转移方程式,但是就本题0-1背包问题来说转移方程式很简单,几乎看模板就行了. 在本题来说WA了很多次,很郁闷,因为我记录v[i]的时候i是从0开始的,一些特殊数据就很尴 ...

  9. poj 23565-Find a multiple

    Find a multiple The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each ...

  10. KMP的正确使用法_x新疆网络赛Query on a string

    Query on a string 题意,给定一个大字符串,给定一个小模式串,定义 两种不同的任务模式,分别是查询和更改: 查询对应区间内,有多少个匹配到位的数字: 修改某一位的某一个字母. 于是直觉 ...