看了半天的同余 扩展欧几里得 练练手

C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27079   Accepted: 7690

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+bx≡c(mod 2^k)
典型的线性同余方程 转化为 bx-y*2^k=c-a 当gcd(b,2^k)能整除c-a的时候 就存在解
通解好求 跑一边exgcd可以得出x0 然后 基础解 x=(c-a)/gcd(b,2^k)*x0; 通解为 x0+zz*(2^k/gcd(2^k,b); 显然当zz=0的时候最小
关键是要求出最小正整数解。
对于ax+by=gcd(a,b) 这个方程 我们有通解 x=x0+b/gcd(a,b);
那么怎么求x的最小正整数解。呢 首先知道一点 b/gcd(a,b)是解的最小区间
这个怎么理解呢

假设c为x的解的最小间距,此时d为y的解的间距,所以x=x0+c*t,y=y0-d*t(x0,y0为一组特解,t为任意整数)

带入方程得:a*x0+a*c*t+b*y0-b*d*t=n,因为a*x0+b*y0=n,所以a*c*t-b*d*t=0,t不等于0时,a*c=b*d

因为a,b,c,d都为正整数,所以用最小的c,d,使得等式成立,ac,bd就应该等于a,b的最小公倍数a*b/gcd(a,b),

所以c=b/gcd(a,b),d就等于a/gcd(a,b)。

所以,若最后所求解要求x为最小整数,那么x=(x0%(b/gcd(a,b))+b/gcd(a,b))%(b/gcd(a,b))即为x的最小整数解。

x0%(b/gcd(a,b))使解落到区间-b/gcd(a,b)~b/gcd(a,b),再加上b/gcd(a,b)使解在区间0~2*b/gcd(a,b),

再模上b/gcd(a,b),则得到最小整数解 (转自http://www.xuebuyuan.com/1380732.html)

学无止境 挺有意思的这个求最小正整数解的方式

#include <cstdio>
#include <iostream>
#include <string> using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==)
{
x=;
y=;
return a;
}
ll temp=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return temp;
}
ll get(ll k)
{
ll temp=;
for(int i=;i<=k;i++)
{
temp*=;
}
return temp;
}
int main()
{
ll a,b,c,k;
while(cin>>a>>b>>c>>k)
{
if(a==&&b==&&c==&&k==) break;
ll temp=b-a;
ll x,y;
ll zz=get(k);
//cout<<zz<<endl;
ll g=exgcd(c,zz,x,y);
//cout<<g<<endl;
if(temp%g!=) cout<<"FOREVER"<<endl;
else
{
ll t=zz/g;//这里有精度问题。。 得多注意
x=(x*(temp/g)%t+t)%t;// 得到目标的最小解
cout<<x<<endl;
}
}
return ;
}

C Looooops的更多相关文章

  1. POJ2115 C Looooops[扩展欧几里得]

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24355   Accepted: 6788 Descr ...

  2. POJ 2115 C Looooops

    扩展GCD...一定要(1L<<k),不然k=31是会出错的 ....                        C Looooops Time Limit: 1000MS   Mem ...

  3. poj 2115 Looooops

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23637   Accepted: 6528 Descr ...

  4. C Looooops(扩展欧几里得)

    C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20128 Accepted: 5405 Descripti ...

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

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

  6. Poj 2115 C Looooops(exgcd变式)

    C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...

  7. C Looooops(扩展欧几里德)

    C Looooops Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  8. poj_2115C Looooops(模线性方程)

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

  9. poj2115 C Looooops(exgcd)

    poj2115 C Looooops 题意: 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束. 若在有限次内结束,则输出循环次数. 否则输出死循环. ...

  10. C Looooops(poj2115+扩展欧几里德)

    C Looooops Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Pr ...

随机推荐

  1. Hbuilder用自有证书打包 ios App上架AppStore流程

    最近在用Hbuilder做跨平台开发,经过一番研究终于在苹果商店上架成功了一款产品!这款产品就很简单,直接用hbuilder打包好,然后上传到商店即可.这里参照ios app提交应用商店 这篇文章结合 ...

  2. 【JDBC】使用Spring提供的JDBCTemplate通过PrepareStatement向MySql数据库插入千万条数据,耗时32m47s,速度提升有限

    数据库环境还和原来一样,只是从Statement换成了PrepareStatement,都说PrepareStatement因为预编译比Statement快,但是实际运行真快不了多少. 代码如下: p ...

  3. [java]将秒数转化为“天时分秒”的格式(转贴+修改)

    public class Time { // format seconds to day hour minute seconds style // Exmplae 5000s will be form ...

  4. JavaScript中"Uncaught TypeError: Cannot set property 'innerHTML' of null"错误

    写了一个函数,在调用时出错:"Uncaught TypeError: Cannot set property 'innerHTML' of null" 代码如下: <!DOC ...

  5. PCL点云库(Point Cloud Library)简介

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=29 什么是PCL PCL(Point Cloud Library)是在吸收了 ...

  6. openstack核心组件--neutron网络服务(4)

    一.neutron 介绍:   Neutron 概述 传统的网络管理方式很大程度上依赖于管理员手工配置和维护各种网络硬件设备:而云环境下的网络已经变得非常复杂,特别是在多租户场景里,用户随时都可能需要 ...

  7. ELK故障处理,不知道成功否

    上周?还是上上周??发现ELK的数据都没有更新了,考虑到这个系统目前不重要,就没有理会.今日再次登陆,发现没有数据更新了!!! system overview 没有主机,没有数据. 登陆系统检查状态, ...

  8. DRF视图-5个扩展类以及GenericAPIView基类

    视图 5个视图扩展类 视图拓展类的作用: 提供了几种后端视图(对数据资源进行曾删改查)处理流程的实现,如果需要编写的视图属于这五种,则视图可以通过继承相应的扩展类来复用代码,减少自己编写的代码量. 这 ...

  9. Dlib支持CPU指令集编译问题(SSE4.2或者AVX)

    The compile script is: mkdir build cd build cmake ../../tools/python -DUSE_SSE2_INSTRUCTIONS=ON cmak ...

  10. 【图像处理】FFmpeg解码H264及swscale缩放详解

      http://blog.csdn.net/gubenpeiyuan/article/details/19548019 主题 FFmpeg 本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何 ...