C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19536   Accepted: 5204

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

题意是问在

for (variable = A; variable != B; variable += C)

这样的情况下,循环多少次。

当中全部的数要mod 2的k次方。所以方程就是(A+C*x)%(2^k)=B,变换一下就是-C*x+(2^k)*y=A-B。解这个方程的最小正数x就可以。

又是扩展欧几里德。



代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; long long yue; void ex_gcd(long long a,long long b, long long &xx,long long &yy)
{
if(b==0)
{
xx=1;
yy=0;
yue=a;
}
else
{
ex_gcd(b,a%b,xx,yy); long long t=xx;
xx=yy;
yy=t-(a/b)*yy;
}
} int main()
{
long long A,B,C,k,k2,xx,yy; while(scanf_s("%lld%lld%lld%lld",&A,&B,&C,&k))
{
if(!A&&!B&&!C&&!k)
break; k2=(1LL<<k);
ex_gcd(-C,k2,xx,yy); if((A-B)%yue)
{
cout<<"FOREVER"<<endl;
}
else
{
xx=xx*((A-B)/yue);
long long r=k2/yue;
if(r<0)
xx=(xx%r-r)%r;
else
xx=(xx%r+r)%r;
printf("%lld\n",xx);
}
}
return 0;
}

POJ 2115:C Looooops的更多相关文章

  1. 【poj 2115】C Looooops(数论--拓展欧几里德 求解同余方程 模版题)

    题意:有一个在k位无符号整数下的模型:for (variable = A; variable != B; variable += C)  statement; 问循环的次数,若"永不停息&q ...

  2. 【题解】POJ 2115 C Looooops (Exgcd)

    POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...

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

  4. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  5. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  6. POJ 2115 C Looooops(模线性方程)

    http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...

  7. poj 2115 C Looooops——exgcd模板

    题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...

  8. poj 2115 Looooops

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23637   Accepted: 6528 Descr ...

  9. Poj 2115 C Looooops(exgcd变式)

    C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...

随机推荐

  1. java.io.ioexception failed to mkdirs jenkins xcode || jenkins 无法创建新文件

    =========================================================== FATAL: Failed to mkdirs: /Users/chenqing ...

  2. 【Linux】Ubuntu配置服务自启动 sysv-rc-conf

    在Ubuntu下,配置服务系统开机自启动,使用的不是chkconfig,而是sysv-rc-conf. 且看如下: 安装: sudo apt-get install sysv-rc-conf 帮助信息 ...

  3. 搭建持续集成单元测试平台(Jenkins+Ant+Java+Junit+SVN)

    一.环境准备 Jenkins: 到官网下载jenkins.war包:http://jenkins-ci.org/ 安装方法有两种: 把下载下来的jenkins.war包放到文件夹下,如C:\jenki ...

  4. 当Ruby的model名字出错时,在现实view时显示错误的提示

    app/controllers/courses_controller.rb:1:in `<top (required)>' app/controllers/courses_controll ...

  5. angularjs显示html片段

    ngBindHtml <div ng-controller="ExampleController"> <p ng-bind-html="myHTML&q ...

  6. python在linux的报错集锦

    1. 报错提示 /usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 ...

  7. Linux中wget用法

    Wget简介:Linux系统中wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTP ...

  8. 【Shell】获取设置日期和延时

    1.读取日期 Linux:~ # date Mon Dec 17 03:16:53 EST 2018 2.格式日期并打印 Linux:~ # date "+%d %B %Y" 17 ...

  9. Quartz.NET开源作业调度框架系列(一):快速入门step by step-转

    Quartz.NET是一个被广泛使用的开源作业调度框架 , 由于是用C#语言创建,可方便的用于winform和asp.net应用程序中.Quartz.NET提供了巨大的灵活性但又兼具简单性.开发人员可 ...

  10. Redability

    在写爬虫进行舆情分析.情感分析等内容时,我们经常爬取博客.新闻等长篇大论的文章,在一个网页上,根据网页布局,人眼可以很明确的确定哪些元素是文章主体.人眼既然能够识别,那么机器必然也是能够识别的.如果给 ...