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 <= x < 2k) modulo 2k.

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 ( <= k <= ) is the number of bits of the control variable of the loop and A, B, C ( <= A, B, C < 2k) 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


Sample Output


FOREVER

Source

 

解题思路:这道题和POJ1061(青蛙约会)一样,都是同余方程的求解,用到了拓展欧几里德算法。而本题题意明确,就是求解这个公式:(a+c*x)mod2^k=b ,求得x 的最小解。变形后可得:c*xmod2^k=b-a,即 c*x=(b-a)mod2^k; 这就是标准的同余方程。

注意:k <=32 ,而 2的 32次方超出整数范围,所以要用__int64或long long ,就不会出现runtime error了。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1000000
#define inf 1e12
ll fac(ll m){
ll ans=;
for(ll i=;i<m;i++){
ans=ans*;
}
return ans;
}
ll e_gcd(ll a,ll b,ll &x,ll &y){
if(b==)
{
x=;
y=;
return a;
}
ll r=e_gcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-a/b*y;
return r;
}
int main()
{
ll a,b,c,k;
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)==){
if(a== && b== && c== && k==){
break;
}
ll x,y,r;
ll d=e_gcd(c,fac(k),x,y);
//printf("---%I64d %I64d %I64d\n",d,x,y);
if((b-a)%d!=){
printf("FOREVER\n");
}
else{
x=x*(b-a)/d;
r=fac(k)/d;
x=(x%r+r)%r;
printf("%I64d\n",x);
}
}
return ;
}

poj 2115 C Looooops(推公式+扩展欧几里得模板)的更多相关文章

  1. [POJ 2115} C Looooops 题解(扩展欧几里德)

    题目描述 对于C的for(i=A ; i!=B ;i +=C)循环语句,给出A,B,C和k(k表示变量是在k进制下的无符号整数),判断循环次数,不能终止输出"FOREVER". 输 ...

  2. poj 1061 青蛙的约会 (扩展欧几里得模板)

    青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status ...

  3. POJ - 1061 青蛙的约会 (扩展欧几里得求同余式)

    题意:两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对 ...

  4. Uva12169 扩展欧几里得模板

    Uva12169(扩展欧几里得) 题意: 已知 $x_i=(a*x_{i-1}+b) mod 10001$,且告诉你 $x_1,x_3.........x_{2t-1}$, 让你求出其偶数列 解法: ...

  5. 扩展欧几里得模板&逆元求法

    拓展欧几里得: 当 gcd ( a , b )= d 时,求绝对值和最小的 x , y 使得 x * a + y * b = d : d = gcd ( a , b ) = gcd ( b , a m ...

  6. POJ 2115 C Looooops( 简单拓欧 + 快速幂 )

    链接:传送门 题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死 ...

  7. POJ 1061 青蛙的约会(扩展欧几里得)

    根据题意,两个青蛙跳到同一个点上才算是遇到了,所以有 (x+m*t) - (y+n*t) = p * ll;  (t是跳的次数,ll是a青蛙跳的圈数跟b青蛙的圈数之差.整个就是路程差等于纬度线周长的整 ...

  8. POJ 1061 青蛙的约会 扩展欧几里得

    扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...

  9. POJ1061 青蛙的约会(扩展欧几里得)

    题目链接:http://poj.org/problem?id=1061 青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submission ...

随机推荐

  1. CSDN 正整数异或值问题

    题目详情: http://student.csdn.net/mcs/programming_challenges?page=4 给你n个正整数,请你计算出有多少对数的异或值小于等于k. 输入描写叙述: ...

  2. HLJOJ1015(多源最短路径失真)

    意甲冠军:n,m,k,有着n村.有着k路,每个村都有一个电话亭,现在,我们要建立在村中心展台,快递每一个需要同村的中心村,然后返回报告(有向图),有着m电话,假设村配置的手机,那么你并不需要报告.最低 ...

  3. iOS8 Core Image In Swift:更复杂的滤镜

    iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift:人脸 ...

  4. apache访问控制设置

    apache访问控制设置 (2009-03-17 11:24:36) 转载▼ 标签: it 杂谈   Order allow,deny    默认情况下禁止所有客户机访问 Order deny,all ...

  5. WEB服务器3--IIS7.0安装和配置

    安装Web服务器(IIS) 点击开始菜单->所有程序->管理工具->服务器管理器,启动服务器管理器,界面如下: 在服务器管理器中,选择角色,你将可以看到角色总视图. 点击添加角色,会 ...

  6. 关于select元素的一些基本知识

    为select元素绑定值的几个方法: 一.通过字符串拼接,让后追加到select元素下, 二.通过DOM创建option元素,为其绑上value值和文本: function loadProvinve( ...

  7. SQL Server事务的存储过程

    在酒店管理系统开发中,我们会创建房间表和房间类型表(房型表)这两个表,如下图所示: 房型表:RoomType 房间表:Room 首先这两个表的关系:Room是从表,RoomType是主表,两表有主外键 ...

  8. .Net Memory -- GC基本知识

    参考资料: http://blogs.msdn.com/b/tess/archive/2008/04/17/how-does-the-gc-work-and-what-are-the-sizes-of ...

  9. python调用ice接口

    今天用python调用ice接口,遇到如下提示 ImportError: No module named Ice 解决方案是 set PYTHONPATH=C:\Program Files\ZeroC ...

  10. Mysql操作个人收集

    1.MySQL修改root密码 mysql> UPDATE user SET Password=PASSWORD('xxxx') where USER='root'; mysql> FLU ...