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 ;
}

最新文章

  1. Sage CRM 平衡区域树结构
  2. HTML5学习小结
  3. 转载:手机网页制作的认识(有关meta标签)
  4. .net C# 图片转Base64 Base64转图片
  5. python扩展实现方法--python与c混和编程 转自:http://www.cnblogs.com/btchenguang/archive/2012/09/04/2670849.html
  6. Effective C++ 3.资源管理
  7. Sorting It All Out
  8. [Practical Git] Filter commit history with git log arguments
  9. codeforces Ilya and Matrix
  10. Button动态样式取代xml
  11. Hexo博客yilia主题添加Gitment评论系统
  12. input做一个开关按钮
  13. python 数据库操作类
  14. postgresql-10.1-3-windows-x64 安装之后,起动pgAdmin 4问题(win10)
  15. svn项目清除svn链接信息
  16. 如何安装ipa文件(二)
  17. 安装GYP(Generate Your Projects)
  18. 【Java面试题】9 abstract class和interface有什么区别?
  19. LRU算法的Python实现
  20. Python通过PhantomJS获取JS渲染后的网页源代码

热门文章

  1. .NET Core和.NET Standard
  2. 在Django中运行脚本文件以及打印出SQL语句。
  3. Bootstrap如何关闭弹窗
  4. linux守护进程与&amp;的区别
  5. error while obtaining ui hierarchy xml file...用 uiautomatorviewer 获取安卓手机软件页面时报错
  6. SikuliI:安装过程(Windows)
  7. hive随机采样
  8. Android ADB 基本命令
  9. 如何解决Android帧动画出现的内存溢出
  10. Flex自定义组件、皮肤,并调用