zoukankan      html  css  js  c++  java
  • POJ2115 C Looooops ——模线性方程(扩展gcd)

    题目链接:http://poj.org/problem?id=2115


    C Looooops
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 27838   Accepted: 7930

    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



    题解:

    A要跳到B的位置,那么它跳了n次后(绕了若干圈),可以跳到B的位置(这时可以理解为A在这一圈内,要从B的位置开始继续跳,当然循环已经结束)。

    可知:( A + n*C ) % len = B。其中len = 1<<k;

    那么:A + n*C = m*len + B,即: n*C - m*len = B - A。 由于m、n是任意常数,所以将其符号全部转为正。

    即得出一个模线性方程: n*C + m*len = B - A。

    标准形式为:a*x + b*y = c,其中 a = C, b = len, c = B-A。

    然后就是扩展欧几里得的应用。


    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 #define ms(a,b) memset((a),(b),sizeof((a)))
    13 //#define LOCAL
    14 using namespace std;
    15 typedef long long LL;
    16 const int INF = 2e9;
    17 const LL LNF = 9e18;
    18 const int mod = 1e9+7;
    19 const int maxn = 1e5+10;
    20 
    21 LL exgcd(LL a, LL b, LL &x, LL &y)
    22 {
    23     if(a==0 &&b==0) return -1;
    24     if(b==0) {x=1; y=0; return a;}
    25     LL d = exgcd(b,a%b,y,x);
    26     y -= a/b*x;
    27     return d;
    28 }
    29 
    30 int main()
    31 {
    32 #ifdef LOCAL
    33     freopen("123", "r", stdin);
    34 //      freopen("output.txt", "w", stdout);
    35 #endif
    36     LL a, b, c, k;
    37     while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k) && (a || b||c ||k))
    38     {
    39         LL len = (1LL<<k);
    40         LL x, y;
    41         LL d = exgcd(c, len, x,y);
    42         LL C = b-a; //C为扩展gcd等式右边的常数项
    43         if(C%d) //如果常数项不能被最大公约数除尽,则无解
    44         {
    45             printf("FOREVER
    ");
    46             continue;
    47         }
    48 
    49         x = (x*(C/d))%len;  //C/d为倍数
    50         x = (x%(len/d)+(len/d))%(len/d);   //(len/d)相当于x的通解的斜率k(必为正数),(x+k)%k即得到最小正整数解
    51         printf("%lld
    ",x);
    52     }
    53 }
    View Code


  • 相关阅读:
    flex datagrid进行删除或增加操作后自动刷新
    java 一个很简单的applet
    转:35岁前必成功的12级跳(男女通用)
    flex DisplayObject UIComponent的区别
    flex flash.utils.Dictionary和Object
    java 正则表达式进行剔除字符
    Flex 元数据标签使用
    使用TableAdapter的Update方法使用注意事项
    android中捕捉menu按键的点击事件
    Repeater 嵌套repeater输出不规则列表
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538639.html
Copyright © 2011-2022 走看看