C Looooops

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

题目大意:

    对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束。

    若在有限次内结束,则输出循环次数。

    否则输出死循环。

解题思路:

    有题意可知:设进行X次循环 A%2^K+(C*X)%2^K=B%2^K

      -->(C*X)%(2^K)=(B-A)%(2^K)

      -->模线性方程 ax=b mod n的解。 其中a=C,b=B-A,n=2^K.

算法:

    算法导论上给出了 求解a=b (mod n)的伪代码 其中a,n为正整数,b为任意整数。

    MODULAR_LINEAR_EQUATION_SOLVER(a,b,n)

    (d,x',y')=EXTENDED_EUCLID(a,n)

    if (d|b)

      x0=x'(b/d) mod n

      for i=0 to d-1

        print (x0+i(n/d)) mod n

    else

      print "no solutions"

    其中x0+i(n/d)为所有可以的解,输出其最小正整数解即可。

Code:

 /*************************************************************************
> File Name: poj2115.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月29日 星期三 13时13分46秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 100000
#define LL long long
using namespace std;
LL extended_gcd(LL a,LL b,LL &x,LL &y)
{
LL ret,tmp;
if (b==)
{
x=,y=;
return a;
}
ret=extended_gcd(b,a%b,x,y);
tmp=x;
x=y;
y=tmp-a/b*y;
return ret;
} int main()
{
LL A,B,C,k;
while (cin>>A>>B>>C>>k)
{
if (A==&&B==&&C==&&k==) break;
LL mod=;
while (k--)
mod*=;
LL x,y;
LL a=C,b=B-A;
LL d=extended_gcd(a,mod,x,y);
//cout<<a<<" "<<x<<" "<<mod<<" "<<y<<endl;
//cout<<d<<endl;
if (b/d*d==b)
{
LL x0=x*(b/d)%mod;
if(x0<) x0+=mod;
//cout<<x0<<endl;
long long Min=x0;
for (int i=;i<=d-;i++)
if ((x0+i*(mod/d))%mod>=)
Min=min(Min,(x0+i*(mod/d))%mod);
cout<<Min<<endl;
}
else
printf("FOREVER\n");
}
return ;
}

    

最新文章

  1. iOS学习-UIButton的imageView和titleLabel
  2. SQL语句实现Split并合并查询结果
  3. html中的 button,input-button, image, input-image的区别
  4. swift学习记录之代理
  5. Win7 Object_Header之TypeIndex解析
  6. codeforces399D
  7. jsp无法支持el标签及jstl标签
  8. angular中的promise
  9. 设置将 Microsoft Azure 的网络基础结构以支持设置为灾难恢复站点
  10. spring整合mybatis错误:Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 62; 文档根元素 &quot;mapper&quot; 必须匹配 DOCTYPE 根 &quot;configuration&quot;。
  11. js获取当前时间并实时刷新
  12. js通過name获取input框输入值
  13. 朱晔的互联网架构实践心得S2E4:小议微服务的各种玩法(古典、SOA、传统、K8S、ServiceMesh)
  14. Xcode9.2打包图片显示异常解决方案
  15. 蜕变成蝶~Linux设备驱动中的阻塞和非阻塞I/O
  16. juqery 下拉加载数据
  17. Android之Sqlite数据库
  18. each的break
  19. sql server 的变量
  20. Jmeter-Maven-Plugin高级应用:Proxy Configuration

热门文章

  1. web响应式之bootstrap的基础用法。
  2. QMessageBox 弹出框上的按钮设置为中文
  3. PLSQL往Oracle数据库插入中文后变为问号 和 启动PLSQL时提示NLS_LANG在客户端不能确定的解决办法
  4. ES6学习笔记(三)
  5. iOS 基础 第五天(0811)
  6. Unity3d + UGUI 的多分辨率适配
  7. C# Windows - ListBox&amp;CheckedListBox
  8. xx创新论坛返工友情项目总结
  9. 3.6 spring-construction-arg 子元素的使用与解析
  10. IOS 数组分组 Grouped NSArray