zoukankan      html  css  js  c++  java
  • C Looooops 同余方程(扩展欧几里得算法)

    Problem 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
    ***************************************************************************************************************************
    ***************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 using namespace std;
     7 typedef  __int64  LL;
     8 LL A,B,C,k;
     9 //欧几里得算法
    10 LL gcd(LL a,LL b)
    11  {
    12      LL c;
    13      if(a<b)
    14      {
    15          c=a;
    16          a=b;
    17          b=c;
    18      }
    19      while(b)
    20      {
    21          c=b;
    22          b=a%b;
    23          a=c;
    24      }
    25      return a;
    26  }
    27  //扩展欧几里得算法
    28  LL  extend_gcd(LL a,LL b,LL &x,LL &y)
    29  {
    30      LL ans,t;
    31      if(b==0)
    32      {
    33          x=1;
    34          y=0;
    35          return a;
    36      }
    37      else
    38      {
    39          ans=extend_gcd(b,a%b,x,y);
    40          t=x;
    41          x=y;
    42          y=t-(a/b)*y;
    43      }
    44      return ans;
    45  }
    46  int main()
    47  {
    48      LL a,b,c,d,x,y,ans;
    49      while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k))
    50      {
    51          if(A+B+C+k==0)
    52           break;
    53          a=C;
    54          b=pow(2,k);
    55          c=B-A;
    56          d=extend_gcd(a,b,x,y);
    57          if(c%d)
    58          {
    59              printf("FOREVER
    ");
    60              continue;
    61          }
    62          ans=(((c*x/d)%b)+b)%(b/d);
    63          if(ans<0)
    64             ans+=(b/d);
    65          printf("%I64d
    ",ans);
    66      }
    67      return 0;
    68  }
    View Code

  • 相关阅读:
    UIrecorder脚本录制与回放
    通过jmeter读取csv的case来实现接口自动化
    Jmeter操作MySQL数据库详解
    使用docker安装Jenkins
    CMMS / EAM与工厂其它业务流程的交集,融合趋势有哪些
    如何让CMMS系统适应您的企业
    CMMS系统中的物联监测
    CMMS系统中工单派案&调度
    移动化的设备维护管理系统--移动端界面
    物流行业设备维护管理系统
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3381808.html
Copyright © 2011-2022 走看看