C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:33752   Accepted: 9832

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
翻译:在循环里,values值为a,每次加c,如果values不等于b,就继续循环下去,死循环输出forever,否则输出循环次数。所有运算都对无符号的k位二进制求模。
解题过程:
设p=2^k
起点为a,每次加c,对p求模若能等于b对p求模则有解,否则无解,输出forever
有解的话设解为x
(a+cx)%p = b%p
a%p + cx%p = b%p
cx%p = (b-a)%p
cx%p + 0 = (b-a)%p
cx%p + yp%p = (b-a)%p
形如ax+by=gcd,扩展欧几里得定理。
 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
// ax + by = gcd(a,b)
ll exgcd(ll a, ll b, ll &x, ll &y)//扩展欧几里德定理
{
if(b==)//终有一次a%b传进来是0,递归出口
{
x=;
y=;
return a;
}
ll q=exgcd(b,a%b,y,x);
//最终递归出来,y1=1,x1=0
y=y-(a/b)*x;
//后面的y相当于下一个递归的x2,x相当于下一个递归的y2,符合推导公式
//x1=y2; y1=x2-[a/b]*y2;
return q;
} int main()
{
ll a,b,c,k,p,x,y;
while(scanf("%lld %lld %lld %lld",&a,&b,&c,&k)!=EOF&&(a+b+c+k))
{
p=;
for(ll i=;i<=k;i++)
p=p*;
ll gcd=exgcd(c,p,x,y);
ll d=b-a;
if(d%gcd)
printf("FOREVER\n");
else
{
ll multiple=d/gcd;///倍数
p=p/gcd;///通解公式:x=x+b/gcd y=y-a/gcd
x=( (x*multiple)%p+p )%p;///求最小正数解
printf("%lld\n",x);
}
}
return ;
}

poj2115-Looooops-(扩展欧几里得定理)的更多相关文章

  1. hdu2669-Romantic-(扩展欧几里得定理)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. poj1061-青蛙的约会-(贝祖定理+扩展欧几里得定理+同余定理)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:132162   Accepted: 29199 Descripti ...

  3. poj 1061(扩展欧几里得定理求不定方程)

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

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

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

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

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

  6. POJ2115 C Looooops[扩展欧几里得]

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24355   Accepted: 6788 Descr ...

  7. C Looooops(扩展欧几里得)

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

  8. POJ 2115 C Looooops(扩展欧几里得)

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

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

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

  10. 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( ...

随机推荐

  1. MySQL ERROR 1300 (HY000): Invalid utf8 character string

    load data报错 原因:原始数据含有 中文\中文 这样的带斜线的字符串. 解决方案:导出的时候替换 replace(d.role_name,'\\','.' ),这样导入时候就不用处理了

  2. 【Linux】【JDK】常用命令使用集和裸机配置JDK步骤。

    使用Zstack创建完成后的linux服务器,使用SSH登录后,就是一下图,可以查看当前路径下的所有文件. 1.常用的命令: 列出当前文件夹下内容:ll 查看目录中的文件 :ls 创建文件夹:mkdi ...

  3. 各种uml图

    UML各种图总结-精华   UML(Unified Modeling Language)是一种统一建模语言,为面向对象开发系统的产品进行说明.可视化.和编制文档的一种标准语言.下面将对UML的九种图+ ...

  4. FreeMarker的空值运算符和逻辑运算符

    1.空值处理运算符 如果你在模板中使用了变量但是在代码中没有对变量赋值,那么运行生成时会抛出异常.但是有些时候,有的变量确实是null,怎么解决这个问题呢? 判断某变量是否存在:“??” 用法为:va ...

  5. Ruby学习笔记1 -- 基本语法和数据类型, Class

    Ruby 有4种数据类型:String, Boolen, Array, Hashes Ruby 有3种操作方法:Method, attribute, ?? Ruby 有xxx: Classes, Ob ...

  6. vnc操作指南

    启动 vncserver : vncserver : -geometry 1905x1005 停止: ps aux | grep vnc kill pid 或者 vncserver -

  7. 【转载】Putty出现 Network error:Software caused connection abort

    一.putty發生 network error 开始菜单进入regedit,尋找 HKEY_CURRENT_USER\Software\SimonTatham 并把这个目录下的子目录全部删除,删除前务 ...

  8. python操作浏览器及截图小结

    近期做网页自动化用到内容小结 1.打开浏览器1)打开默认配置的浏览器from selenium import webdriverdriver = webdriver.Firefox()"&q ...

  9. hive命令的执行方式

    1.通过cli直接执行 2.hive -e "hql" 如:[root@host ~]# hive -e "use gamedw;show tables" [r ...

  10. 1. 配置win7下odbc数据源找不到数据库驱动的问题

    win7下ODBC数据源DB2的链接 直接在控制面板---管理工具----数据源(ODBC) 打开数据源配置,发现只有SQLServer的驱动,其他的都没有了. 解决方法是C:\Windows\Sys ...