Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32062   Accepted: 9337

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

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

 
  在模2^k的意义下,A每次加C,最少加多少次能达到B,输出这个次数。
  易得 A+i*C= B  (mod 2^k)   -->   i*C+j*(2^k) = B - A ,问题转化为求这个方程成立的最小的i值,直接拓欧,坑点是K最大32,不强制LL
的话会爆。
  

 #include<iostream>
#include<cstdio>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define inf 0x3f3f3f3f
void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){d=a;x=;y=;}
else{exgcd(b,a%b,d,y,x);y-=(a/b)*x;}
}
int main(){
LL A,B,C,K;
while(scanf("%lld%lld%lld%lld",&A,&B,&C,&K)!=EOF){
if(A==&&B==&&C==&&K==) break;
LL x,y,d;
LL M=;
M<<=K;
exgcd(C,M,d,x,y);
if((B-A)%d!=){
puts("FOREVER");
}
else{
M/=d;
printf("%lld\n",((x*(B-A)/d)%M+M)%M);
}
}
return ;
}

poj-2115-exgcd的更多相关文章

  1. R - C Looooops POJ - 2115 (exgcd)

    题目大意:很好理解,一个for循环语句,从a开始到b结束,步长是c,模数是pow(2,k) 问,最少循环多少次,才能到达b,如果永远都到不了b,输出FOREVER 题解:其实就是求一个线性方程,cx= ...

  2. 【题解】POJ 2115 C Looooops (Exgcd)

    POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...

  3. poj 2115 C Looooops——exgcd模板

    题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...

  4. POJ 2115 C Looooops(扩展欧几里得应用)

    题目地址:POJ 2115 水题. . 公式非常好推.最直接的公式就是a+n*c==b+m*2^k.然后能够变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod( ...

  5. POJ 2115 C Looooops(Exgcd)

    [题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...

  6. Poj 2115 C Looooops(exgcd变式)

    C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...

  7. C Looooops POJ - 2115 (exgcd)

    一个编译器之谜:我们被给了一段C++语言风格的循环 for(int i=A;i!=B;i+=C) 内容; 其中所有数都是k位二进制数,即所有数时膜2^k意义下的.我们的目标时球出 内容 被执行了多少次 ...

  8. POJ 2115 C-Looooops | exgcd

    题目 给出一个循环for(int i=A;i!=B;i+=C) 在mod (1<<k) 下是否可以退出循环 是,输出时间,否输出FORVEER 题解: 题意可以变换成 A+Cx=B (mo ...

  9. POJ 2115 C Looooops(模线性方程)

    http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...

  10. poj 2115 扩展欧几里得

    题目链接:http://poj.org/problem?id=2115 题意: 给出一段循环程序,循环体变量初始值为 a,结束不等于 b ,步长为 c,看要循环多少次,其中运算限制在 k位:死循环输出 ...

随机推荐

  1. Python 安装与环境变量配置

    一.软件下载 Python安装包下载地址:https://www.python.org/ 二.安装过程(略) 三.环境变量配置: 方法一:使用cmd命令添加path环境变量 在cmd下输入: path ...

  2. HBase与列存储

    传统的行存储和(HBase)列存储的区别 1.为什么要按列存储 列式存储(Columnar or column-based)是相对于传统关系型数据库的行式存储(Row-basedstorage)来说的 ...

  3. 将tiff文件转化为jpg文件并保存

    jar包准备 jai-codec和jai-core 主要过程 private boolean parseTifFile(FileItem item) { logger.info("----- ...

  4. Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)

    http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...

  5. File操作-将数据库里的数据写入到指定路径的txt文件里

    package com.Cristin.File;//将数据库里的数据写入到指定路径的txt文件里 import java.io.File;import java.io.FileOutputStrea ...

  6. ImgNoGoodWindow

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using UnityEditor; ...

  7. VMware网络连接模式—桥接、NAT以及仅主机模式的详细介绍和区别

    在使用VMware Workstation(以下简称:VMware)创建虚拟机的过程中,配置虚拟机的网络连接是非常重要的一环,当我们为虚拟机配置网络连接时,我们可以看到如下图所示的几种网络连接模式:桥 ...

  8. 1.0 poi单元格合合并及写入

    最近项目中用到poi生成Excel时,用到了单元格合并,于是参考了http://www.anyrt.com/blog/list/poiexcel.html写的文章,但是其中有些地方不是很清楚,于是自己 ...

  9. javaSE习题 第三章 运算符、表达式和语句

    问答: 1.下列System.out.printf的结果是什么? int a=100,x,y; x=++a; y=a--; System.out.printf("%d,%d,%d" ...

  10. ip啊

    网络模型被OSI分成七层,TCP/IP协议大致对应了2.3.4.7层,分别是数据链路层.网络层.传输层.应用层,IP协议处于网络层上,它的工作原理说白了并不复杂: 整个互联网上所有的机器都有唯一一个I ...