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


  • 相关阅读:
    TortoiseCVS + 错误 + 无效句柄:解决方法
    Oracle学习笔记:关于Oracle服务器在windows32位平台上连接数受限制的问题
    IE6 很邪恶,但我爱它的盒子模型
    PHP环境搭建:Windows 7下安装配置PHP+Apache+Mysql环境教程
    关于跨浏览器测试那点事
    【转】IETester更新至最新版已经兼容Windows7(附下载地址及Debugbar插件)
    Web 设计师的 50 个超便利工具(上)
    各大浏览器 CSS3 和 HTML5 兼容速查表
    15 个 JavaScript Web UI 库
    编写跨浏览器兼容的 CSS 代码的金科玉律
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538639.html
Copyright © 2011-2022 走看看