题目链接: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。

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

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
//#define LOCAL
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 1e5+; LL exgcd(LL a, LL b, LL &x, LL &y)
{
if(a== &&b==) return -;
if(b==) {x=; y=; return a;}
LL d = exgcd(b,a%b,y,x);
y -= a/b*x;
return d;
} int main()
{
#ifdef LOCAL
freopen("", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
LL a, b, c, k;
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k) && (a || b||c ||k))
{
LL len = (1LL<<k);
LL x, y;
LL d = exgcd(c, len, x,y);
LL C = b-a; //C为扩展gcd等式右边的常数项
if(C%d) //如果常数项不能被最大公约数除尽,则无解
{
printf("FOREVER\n");
continue;
} x = (x*(C/d))%len; //C/d为倍数
x = (x%(len/d)+(len/d))%(len/d); //(len/d)相当于x的通解的斜率k(必为正数),(x+k)%k即得到最小正整数解
printf("%lld\n",x);
}
}

POJ2115 C Looooops ——模线性方程(扩展gcd)的更多相关文章

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

  2. poj_2115C Looooops(模线性方程)

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

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

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

  4. C Looooops(扩展欧几里得求模线性方程)

    http://poj.org/problem?id=2115 题意:对于C的循环(for i = A; i != B; i+=C)问在k位存储系统内循环多少次结束: 若循环有限次能结束输出次数,否则输 ...

  5. POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))

    d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...

  6. C Looooops(扩展欧几里得+模线性方程)

    http://poj.org/problem?id=2115 题意:给出A,B,C和k(k表示变量是在k位机下的无符号整数),判断循环次数,不能终止输出"FOREVER". 即转化 ...

  7. POJ 1061 青蛙的约会(扩展GCD求模线性方程)

    题目地址:POJ 1061 扩展GCD好难懂.. 看了半天.最终把证明什么的都看明确了. .推荐一篇博客吧(戳这里),讲的真心不错.. 直接上代码: #include <iostream> ...

  8. POJ 2115 C Looooops(模线性方程)

    http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...

  9. [ACM_其他] Modular Inverse [a关于模m的逆 模线性方程]

    Description The modular modular multiplicative inverse of an integer a modulo m is an integer x such ...

随机推荐

  1. 洛谷——P1126 机器人搬重物

    P1126 机器人搬重物 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一个直径1.6米的球.在试验阶段,机器人被用于在一个储藏室中搬运货物.储藏室是一个N*M的网格,有 ...

  2. 串口VMIN VTIME 详解

    原文地址: 以前跟着做过VxWorks的开发,主要通信方式是串口,因为底层BSP包已经做好了,串口通信非常简单.后来接触Linux,在一块OK6410上跑Linux串口通信,才发现原来天真的以为甚是简 ...

  3. mac 下删除xcode后使用git

    1. http://blog.bobbyallen.me/2014/03/07/how-to-install-git-without-having-to-install-xcode-on-macosx ...

  4. android 扩大view的响应区域

    1.Android提供TouchDelegate帮助实现扩大一个很小的view的点击区域 例如:https://developer.android.com/training/gestures/view ...

  5. java中正则表达式要进行转义的字符。

    /** * 转义正则特殊字符 ($()*+.[]?\^{},|) * * @param keyword * @return */public static String escapeExprSpeci ...

  6. Ubuntu 16.04安装MongoDB的GUI工具RoboMongo

    一.下载: https://robomongo.org/download 离线版本:(链接: https://pan.baidu.com/s/1mirFi56 密码: y3t2) 二.安装: -lin ...

  7. GOF23种设计模式-工厂模式

    • 工厂模式: – 实现了创建者和调用者的分离. – 详细分类: • 简单工厂模式 • 工厂方法模式 • 抽象工厂模式 • 面向对象设计的基本原则: – OCP(开闭原则,Open-Closed Pr ...

  8. 第九讲_图像生成 Image Captioning

    第九讲_图像生成 Image Captioning 生成式对抗网络 Generative Adversarial network 学习数据分布:概率密度函数估计+数据样本生成 生成式模型是共生关系,判 ...

  9. K-L变换和 主成分分析PCA

    一.K-L变换 说PCA的话,必须先介绍一下K-L变换了. K-L变换是Karhunen-Loeve变换的简称,是一种特殊的正交变换.它是建立在统计特性基础上的一种变换,有的文献也称其为霍特林(Hot ...

  10. 为什么应使用 Node.js

    为什么应使用 Node.js JavaScript 高涨的人气带来了很多变化,以至于如今使用其进行网络开发的形式也变得截然不同了.就如同在浏览器中一样,现在我们也可以在服务器上运行 JavaScrip ...