C Looooops

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23700   Accepted: 6550

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

由题意易得(a+cx)%2^k==b,求x最小值。可得同余方程c*x=(b-a)mod2^k。

 //2016.8.17
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long using namespace std; ll ex_gcd(ll a, ll b, ll& x, ll& y)//扩展欧几里得
{
if(b==)
{
x = ;
y = ;
return a;
}
ll ans = ex_gcd(b, a%b, x, y);
ll tmp = x;
x = y;
y = tmp-(a/b)*y;
return ans;
} int main()
{
ll a, b, c, x, y, res, n;
int k;
while(scanf("%lld%lld%lld%d", &a, &b, &c, &k)!=EOF)
{
if(!a&&!b&&!c&&!k)
break;
n = (ll)<<k;
res = ex_gcd(c, n, x, y);
cout<<res<<endl<<x<<endl;
if((b-a)%res!=)cout<<"FOREVER"<<endl;
else
{
x = x*(b-a)/res%n;//方程ax=b-a(mod n)的最小解
ll tmp = n/res;
x = (x%tmp+tmp)%tmp;//最小正数解
printf("%lld\n", x);
}
} return ;
}
 #include <iostream>
#define ll long long using namespace std; ll ex_gcd(ll a, ll b, ll& x, ll& y){
if(b == ){
x = ;
y = ;
return a;
}
ll ans = ex_gcd(b, a%b, x, y);
ll tmpx = x;
x = y;
y = tmpx-a/b*y;
return ans;
} int main()
{
int a, b, c, k;
while(cin>>a>>b>>c>>k){
if(!a&&!b&&!c&&!k)break;
ll x, y;
ll A = c;
ll B = b-a;
ll n = 1LL<<k;
ll gcd = ex_gcd(A, n, x, y);
if(B%gcd != )
cout<<"FOREVER"<<endl;
else{
x = (x*(B/gcd))%n;
x = (x%(n/gcd)+n/gcd)%(n/gcd);
cout<<x<<endl;
}
}
return ;
}

POJ2115(扩展欧几里得)的更多相关文章

  1. 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 ...

  2. POJ2115 - C Looooops(扩展欧几里得)

    题目大意 求同余方程Cx≡B-A(2^k)的最小正整数解 题解 可以转化为Cx-(2^k)y=B-A,然后用扩展欧几里得解出即可... 代码: #include <iostream> us ...

  3. 【扩展欧几里得】poj2115 C Looooops

    题意大概是让你求(A+Cx) mod 2^k = B的最小非负整数解. 若(B-A) mod gcd(C,2^k) = 0,就有解,否则无解. 式子可以化成Cx + 2^k*y = B - A,可以用 ...

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

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

  5. POJ1061:青蛙的约会+POJ2115C Looooops+UVA10673Play with Floor and Ceil(扩展欧几里得)

    http://poj.org/problem?id=1061 第一遍的写法: #include <iostream> #include <stdio.h> #include & ...

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  7. UVA 12169 Disgruntled Judge 枚举+扩展欧几里得

    题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...

  8. UVA 10090 Marbles 扩展欧几里得

    来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...

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

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

随机推荐

  1. PAT (Advanced Level) 1026. Table Tennis (30)

    情况比较多的模拟题. 交了50发的样子才AC......AC之后我的天空星星都亮了. #include<iostream> #include<cstring> #include ...

  2. CodeForces 614C Peter and Snow Blower

    简单计算几何,只要算出圆心到多边形上的最短距离和最长距离即可 #include<cstdio> #include<cstring> #include<cmath> ...

  3. Java中Date对象与String互转

    package com.java.test; import java.text.ParseException; import java.text.SimpleDateFormat; import ja ...

  4. [iOS]C语言技术视频-09-枚举的定义

    下载地址: 链接: http://pan.baidu.com/s/1o625Ee2 密码: 8kp5

  5. iOS之Nib和Xib以及storyboard(故事版)

    本文转发至:http://blog.csdn.net/tonny_guan/article/details/8542789 nib.xib与故事板 如果大家使用过苹果的官方资料,一定会发现某些资料上会 ...

  6. 照着例子学习 protobuf-lua

    参考文章:cocos2dx使用lua和protobuf 首先得下载protobuf-gen-lua的插件,插件Git地址在此. 下载完之后进入到protoc-gen-lua\plugin这个目录,并在 ...

  7. 在Action类中获得HttpServletResponse对象的四种方法

    在struts1.xAction类的execute方法中,有四个参数,其中两个就是response和request.而在Struts2中,并没有任何参数,因此,就不能简单地从execute方法获得Ht ...

  8. COM问题

    因为应用程序正在发送一个输入同步呼叫,所以无法执行传出的呼叫.

  9. 1、Sencha cmd学习笔记(一) 使你的sencha cmd跑起来

    带着Ext JS 5来使用sencha cmd -------------------------------------------------------------------  这个指导通过处 ...

  10. C#webbrowser控件技巧(取得javascript变量值,禁止显示脚本错误)

    C#中的webbrowser控件比较好用. 下面本人搜索整理的几个小技巧. 1. 从C#中取得javascript的变量值. using mshtml;using System.Reflection; ...