POJ2115 C Looooops[扩展欧几里得]

时间:2022-09-01 14:01:33
C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24355   Accepted: 6788

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


(A+s*C)%2^k=B
(A+s*C)≡B(mod 2^k)
s*C-m*2^k=B-A
ax+by=c
有一个问题,b没必要是负的,反正正负a和b的线性组合集都一样,况且此题不需要y
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll A,B,C,k;
inline void exgcd(ll a,ll b,ll &g,ll &x,ll &y){
if(b==){x=;y=;g=a;}
else{exgcd(b,a%b,g,y,x);y-=x*(a/b);}
}
int main(){
while(scanf("%lld%lld%lld%lld",&A,&B,&C,&k)!=EOF){
if(!A&&!B&&!C&&!k) break;
ll c=B-A,a=C,b=1LL<<k,g,x,y;
exgcd(a,b,g,x,y);
if(c%g) printf("FOREVER\n");
else{
b/=g;c/=g;
printf("%lld\n",(x%b*c%b+b)%b);
}
}
}

POJ2115 C Looooops[扩展欧几里得]的更多相关文章

  1. poj2115 C Looooops——扩展欧几里得

    题目:http://poj.org/problem?id=2115 就是扩展欧几里得呗: 然而忘记除公约数... 代码如下: #include<iostream> #include< ...

  2. C Looooops&lpar;扩展欧几里得&plus;模线性方程&rpar;

    http://poj.org/problem?id=2115 题意:给出A,B,C和k(k表示变量是在k位机下的无符号整数),判断循环次数,不能终止输出"FOREVER". 即转化 ...

  3. &lbrack;POJ2115&rsqb;C Looooops 拓展欧几里得

    原题入口 这个题要找到本身的模型就行了 a+c*x=b(mod 2k) ->  c*x+2k*y=b-a 求这个方程对于x,y有没有整数解. 这个只要学过拓展欧几里得(好像有的叫扩展欧几里德QA ...

  4. C Looooops&lpar;扩展欧几里得&rpar;

    C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20128 Accepted: 5405 Descripti ...

  5. POJ 2115 C Looooops&lpar;扩展欧几里得&rpar;

    辗转相除法(欧几里得算法) 时间复杂度:在O(logmax(a, b))以内 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a ...

  6. POJ 2115 C Looooops扩展欧几里得

    题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #incl ...

  7. POJ 2115 C Looooops&lpar;扩展欧几里得应用)

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

  8. POJ - 2115C Looooops 扩展欧几里得(做的少了无法一眼看出)

    题目大意&&分析: for (variable = A; variable != B; variable += C) statement;这个循环式子表示a+c*n(n为整数)==b是 ...

  9. POJ2115 C Looooops 模线性方程(扩展欧几里得)

    题意:很明显,我就不说了 分析:令n=2^k,因为A,B,C<n,所以取模以后不会变化,所以就是求(A+x*C)%n=B 转化一下就是求 C*x=B-A(%n),最小的x 令a=C,b=B-A ...

随机推荐

  1. 01&period;Apache FtpServer配置

    1.解压Apache FTPServer 将下载下来的压缩包(ftpserver-1.0.6.zip)解压到本地,其目录结构如下图: 2.修改users.properties 修改 \apache-f ...

  2. WebDriverWait自定义等待事件

    1. webDriverWait自定义WebElement类事件 public WebElement waitForElementVisible(WebDriver driver,final By l ...

  3. C&plus;&plus;11里面的Lambda表达式

    Lambda Expressions in C++ C++中的Lambda表达式 In Visual C++, a lambda expression—referred to as a lambda— ...

  4. 修改OpenSSL默认编译出的动态库文件名称

    在 Windows 平台上调用动态链接库 dll 文件时,有两种方式:a) 隐式的加载时链接:使用 *.lib (导入库)文件,在 IDE 的链接器相关设置中加入导入库 lib 文件的名称,或在程序中 ...

  5. asp&period;net mvc异步查询

    对于asp.net mvc异步查询 如何做MVC异步查询,做列表页面. 查询是项目中必不可少的工作,而且不同的项目不同的团队,都有自己的简单方法.Asp.net mvc 有自己独特的优势,下面是结合m ...

  6. mysql 命令语句

    1.大部分数据库命令语句 数据库的命令 net start MYsql 启动的服务 net stop mysql 关闭服务 mysql -uroot -p 输入数据库名和密码登入 show datab ...

  7. QQ使用的使用评价

    1:界面以及功能:打开软件之后探出登录窗口,基本功能的登录,找回密码,注册帐号等功能均在比较醒目的位置,界面较为友好. 将注册帐号放在打开软件的第一界面当然是正确的选择,给予用户非常直观的提示(没有帐 ...

  8. 51Nod 算法马拉松28 A题 先序遍历与后序遍历 分治

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - 51Nod1832 题意概括 对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的二叉树. 两棵二 ...

  9. Java ThreadLocal的使用

    Java中的ThreadLocal类允许我们创建只能被同一个线程读写的变量.因此,如果一段代码含有一个ThreadLocal变量的引用,即使两个线程同时执行这段代码,它们也无法访问到对方的Thread ...

  10. 修改控制台为Consolas字体

    windows下控制台字体修改为Consolas字体比较好看,修改步骤如下: 临时修改 命令行cmd命令进入控制台,输入chcp 437命令,执行. 右键点击标题栏进入属性,修改字体为Consolas ...