C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29262   Accepted: 8441

Description

A Compiler Mystery: We are given a C-language style for loop of type

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

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

  1. 3 3 2 16
  2. 3 7 2 16
  3. 7 3 2 16
  4. 3 4 2 16
  5. 0 0 0 0

Sample Output

  1. 0
  2. 2
  3. 32766
  4. FOREVER

Source

大致题意:求在mod 2^k下的一个for循环要运行多少次.
分析:列出同余式,扩展欧几里得解决.
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. typedef long long ll;
  9. ll a,b,c,k,A,B,C,d;
  10.  
  11. ll qpow(ll b)
  12. {
  13. ll res = ,x = ;
  14. while (b)
  15. {
  16. if (b & )
  17. res *= x;
  18. x *= x;
  19. b >>= ;
  20. }
  21. return res;
  22. }
  23.  
  24. ll gcd(ll a,ll b)
  25. {
  26. if (!b)
  27. return a;
  28. return gcd(b,a % b);
  29. }
  30.  
  31. void exgcd(ll a,ll b,ll &x,ll &y)
  32. {
  33. if (!b)
  34. {
  35. x = ;
  36. y = ;
  37. return;
  38. }
  39. exgcd(b,a % b,x,y);
  40. ll t = x;
  41. x = y;
  42. y = t - (a / b) * y;
  43. }
  44.  
  45. int main()
  46. {
  47. while (scanf("%lld%lld%lld%lld",&a,&b,&c,&k) == )
  48. {
  49. if (!a && !b && !c && !k)
  50. break;
  51. C = b - a;
  52. A = c;
  53. B = qpow(k);
  54. d = gcd(A,B);
  55. if (C % d != || (b >= B || a >= B || c >= B))
  56. printf("FOREVER\n");
  57. else
  58. {
  59. ll x,y;
  60. exgcd(A,B,x,y);
  61. x = x * C / d;
  62. B /= d;
  63. x = (x % B + B) % B;
  64. printf("%lld\n",x);
  65. }
  66. }
  67.  
  68. return ;
  69. }

poj2115 C Looooops的更多相关文章

  1. poj2115 C Looooops(exgcd)

    poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...

  2. POJ2115——C Looooops(扩展欧几里德+求解模线性方程)

    C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (vari ...

  3. POJ2115 C Looooops[扩展欧几里得]

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24355   Accepted: 6788 Descr ...

  4. POJ2115 C Looooops 扩展欧几里德

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...

  5. POJ2115 C Looooops ——模线性方程(扩展gcd)

    题目链接:http://poj.org/problem?id=2115 C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  6. POJ2115 C Looooops(线性同余方程)

    无符号k位数溢出就相当于mod 2k,然后设循环x次A等于B,就可以列出方程: $$ Cx+A \equiv B \pmod {2^k} $$ $$ Cx \equiv B-A \pmod {2^k} ...

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

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

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

  9. POJ2115 C Looooops(数论)

    题目链接. 分析: 数论了解的还不算太多,解的时候,碰到了不小的麻烦. 设答案为x,n = (1<<k), 则 (A+C*x) % n == B 即 (A+C*x) ≡ B (mod n) ...

随机推荐

  1. 廖雪峰git笔记

    查看本地机子的在Git上的名字和邮箱:git config user.namegit config user.email 对所有仓库指定相同的用户名和Email地址:git config --glob ...

  2. Linux命令之tar命令

    [root@linux ~]# tar [-cxtzjvfpPN] 文件与目录 .... 参数: -c :建立一个压缩文件的参数指令(create 的意思): -x :解开一个压缩文件的参数指令! - ...

  3. Kubernetes-----Endpoints

    Endpoints是实现实际服务的端点集合. Kubernetes在创建Service时,根据Service的标签选择器(Label Selector)来查找Pod,据此创建与Service同名的En ...

  4. centos 7 install gnome etc

    centos yum 有grouplist子命令,可以查看当前系统有多少软件组件,里面就有gnome:"GNOME Desktop" sudo yum groupinstall G ...

  5. Python基础知识-05-数据类型总结字典

    python其他知识目录 1.一道题,选择商品的序号.程序员和用户各自面对的序号起始值 如有变量 googs = ['汽车','飞机','火箭'] 提示用户可供选择的商品: 0,汽车1,飞机2,火箭用 ...

  6. linux计划任务 学习笔记

    原文链接: http://www.tsingfeng.com/?tag=cronjob 本文说的计划任务是指linux的Cronjob.语法 下面是个简单的计划任务: 10 * * * * /usr/ ...

  7. 0329--Scrum团队准备工作

    一.团队名称,团队目标.团队口号.团队照 1.团队名称:Blackhriar 2.团队目标:完成一个完善的,可以投入市场供用户使用,甚至具有一定商业价值的项目~come on! 3.团队口号:抱怨事件 ...

  8. 02慕课网《进击Node.js基础(一)》——CommonJs标准

    是一套规范管理模块 每个js 为一个模块,多个模块作为一个包 node.js和Couchdb是对其的实现: 不同于jQuery 模块:定义.标识.引用(地址/模块名称) 模块类型: 核心模块http ...

  9. IT行业所面临的问题

    在阅读了“2015 IT行业大学生就业分析报告”和“2014年十大最热门行业和职业排行榜 IT行业最吃香_联展新闻”两则新闻后,我决定用一篇和老师对话的形式来表达我的感受. dym:人潮汹涌的招聘市场 ...

  10. 201621123037 《Java程序设计》第3周学习总结

    #Week03-面向对象入门 1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联 ...