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. Sebastian Ruder : NLP 领域知名博主博士论文面向自然语言处理的神经网络迁移学习

    Sebastian Ruder 博士的答辩 PPT<Neural Transfer Learning for Natural Language Processing>介绍了面向自然语言的迁 ...

  2. ORA-28000账户被锁和解锁

    sqlplus "/as sysdba" 或者 sqlplus /nolog --不在cmd或者terminal当中暴露密码的登陆方式 conn /as sysdba 查看用户信息 ...

  3. Postgresql ERROR: permission denied for relation app_info

    启用终端,: 进入mydb数据库:\c mydb 然后给当前数据库的角色赋予权限:GRANT ALL PRIVILEGES ON TABLE 表名  TO 角色名;

  4. 【转】 .Net MVC4 上传大文件,并保存表单

    1前台:cshtml </pre><pre name="code" class="csharp">@model BLL.BLL.Prod ...

  5. mysql错误:Column count doesn't match value count at row 1

    mysql错误:Column count doesn't match value count at row 1 mysql错误:Column count doesn't match value cou ...

  6. 《算法》第二章部分程序 part 2

    ▶ 书中第二章部分程序,加上自己补充的代码,包括若干种归并排序,以及利用归并排序计算数组逆序数 ● 归并排序 package package01; import java.util.Comparato ...

  7. tomcat7的一些设置(修改内存)

    1.内存修改.今天在tomcat7下面部署了两个项目.居然报错了. 然后开始打开Tomcat7w.exe 在java标签中的initial memory pool和muxinum memory poo ...

  8. Maven的下载和配置

    一.下载 打开 链接地址 http://maven.apache.org/download.cgi 下载 Maven, 下载加压打开以后目录: 二.配置环境变量 我的电脑 -> 属性->高 ...

  9. Android自定义View学习(三)

    属性动画(上) 参考:HenCoder 自定义绘制的第 1-6 期:属性动画 Property Animation(上手篇) Interpolator 其实就是速度设置器,设置动画运行的速度. 属性动 ...

  10. react-native android 报错 error calling Appregistry.runApplication

    解决了权限问题以为就没问题了,但是进来就红屏了,报错信息如下: 解决了,懒得截图了 error calling Appregistry.runApplication 这个问题也找了很久,开始找到 ht ...