zoukankan      html  css  js  c++  java
  • POJ 2115 C Looooops 扩展欧几里得

    C Looooops
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15066   Accepted: 3820

    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

    Source

     
     1 /*
     2 题意:
     3 (A+Cx)%2^k=B ==> A+Cx = 2^k*y+B
     4 ==> Cx-2^k*y=B-A;
     5 形如 ax+by=c
     6 a=C
     7 b=2^k
     8 c=B-A;
     9 
    10 */
    11 
    12 #include<iostream>
    13 #include<cstdio>
    14 #include<cstring>
    15 #include<cstdlib>
    16 using namespace std;
    17 
    18 __int64 f[33];
    19 void make_hxl()
    20 {
    21     int i;
    22     f[0]=1;
    23     f[1]=2;
    24     for(i=2;i<=32;i++)
    25     f[i]=f[i-1]*2;
    26 }
    27 
    28 __int64 Ex_gcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
    29 {
    30     if(b==0)
    31     {
    32         x=1;
    33         y=0;
    34         return a;
    35     }
    36     __int64 g=Ex_gcd(b,a%b,x,y);
    37     __int64 hxl=x-(a/b)*y;
    38     x=y;
    39     y=hxl;
    40     return g;
    41 }
    42 
    43 void make_ini(__int64 A,__int64 B,__int64 C,__int64 k)
    44 {
    45     __int64 c,a,b,x,y,d;
    46     a=C;
    47     b=f[k];
    48     c=B-A;
    49 
    50     d=Ex_gcd(a,b,x,y);
    51     if(c%d!=0)
    52     {
    53         printf("FOREVER
    ");
    54         return;
    55     }
    56     x=c/d*x;
    57     b=b/d;
    58     x=(x%b+b)%b;
    59     printf("%I64d
    ",x);
    60 }
    61 int main()
    62 {
    63     __int64 A,B,C,k;
    64     make_hxl();
    65     while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)>0)
    66     {
    67         if(A==0 && B==0 && C==0 &&k==0)
    68         break;
    69         make_ini(A,B,C,k);
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    yum -y list java* 查看当前java的版本
    max number of threads [1024] for user [lish] likely too low, increase to at least [2048]
    max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
    max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
    linux文件描述符open file descriptors与open files的区别
    samba 报错
    检测端口是否开启
    sqlserver2008出现数据库主体在该数据库中拥有架构,无法删除的解决方案
    sqlserver生成表结构文档的方法
    胡萝卜周博客 软件下载
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3263927.html
Copyright © 2011-2022 走看看