C Looooops
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22260   Accepted: 6125

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

 

大致题意:

对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束。

若在有限次内结束,则输出循环次数。

否则输出死循环。

解题思路:

题意不难理解,只是利用了 k位存储系统 的数据特性进行循环。

例如int型是16位的,那么int能保存2^16个数据,即最大数为65535(本题默认为无符号),

当循环使得i超过65535时,则i会返回0重新开始计数

如i=65534,当i+=3时,i=1

其实就是 i=(65534+3)%(2^16)=1

有了这些思想,设对于某组数据要循环x次结束,那么本题就很容易得到方程:

x=[(B-A+2^k)%2^k] /C

即 Cx=(B-A)(mod 2^k)  此方程为 模线性方程,本题就是求X的值。

下面将结合《算法导论》第2版进行简述,因此先把上面的方程变形,统一符号。

令a=C

b=B-A

n=2^k

那么原模线性方程变形为:

ax=b (mod n)

该方程有解的充要条件为 gcd(a,n) | b ,即 b% gcd(a,n)==0

令d=gcd(a,n)

有该方程的 最小整数解为 x = e (mod n/d)

其中e = [x0 mod(n/d) + n/d] mod (n/d) ,x0为方程的最小解

那么原题就是要计算b% gcd(a,n)是否为0,若为0则计算最小整数解,否则输出FOREVER

当有解时,关键在于计算最大公约数 d=gcd(a,n) 与 最小解x0

参考《算法导论》,引入欧几里得扩展方程  d=ax+by ,

通过EXTENDED_EUCLID算法(P571)求得d、x、y值,其中返回的x就是最小解x0,求d的原理是辗转相除法(欧几里德算法)

再利用MODULAR-LINEAR-EQUATION-SOLVER算法(P564)通过x0计算x值。注意x0可能为负,因此要先 + n/d 再模n/d。

以上方法的推导过程大家自己看《算法导论》。。。这里不证明,只直接使用。

注意:

计算n=2^k时,用位运算是最快的,1<<k (1左移k位)就是2^k

但是使用long long的同学要注意格式, 1ll<<k

使用__int64的同学要强制类型转换 (__int64)1<<k

不然会WA

 TLE代码:

#include<cstdio>
#include<iostream>
using namespace std;
#define ll long long
ll gcd(ll a,ll b){
if(!b) return a;
return gcd(b,a%b);
}
ll quick_pow(ll x,ll n){
if(n==) return ;
else{
while(!(n&)){
n>>=;
x*=x;
}
}
ll result=x;
n>>=;
while(n){
x*=x;
if((n&)){
result*=x;
}
n>>=;
}
return result;
}
int main(){
ll a,b,c,k;
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)==){
if(!a&&!b&&!c&&!k) break;
ll d=gcd(c,b-a);
if(b%d==){puts("FOREVER");continue;}
ll mod=quick_pow(,k);
printf("%I64d\n",(b-a+mod)%mod/c);
}
return ;
}

 AC代码:

#include<iostream>
#include<cstdio>
using namespace std;
#define LL long long
//d=ax+by,其中最大公约数d=gcd(a,n),x、y为方程系数,返回值为d、x、y
LL gcd(LL a,LL b,LL& x,LL& y){
if(b==){
x=;y=; //d=a,x=1,y=0,此时等式d=ax+by成立
return a;
}
LL d=gcd(b,a%b,x,y);
LL xt=x;
x=y;
y=xt-a/b*y;//系数x、y的取值是为满足等式d=ax+by
return d;
}
int main(){
LL A,B,C,k;
while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)==){
if(!A&&!B&&!C&&!k) break;
LL a=C;
LL b=B-A;
LL n=1ll<<k;
LL x,y;
LL d=gcd(a,n,x,y);//求a,n的最大公约数d=gcd(a,n)和方程d=ax+by的系数x、y
if(b%d!=) puts("FOREVER");//方程 ax=b(mod n) 无解
else{
x=(x*(b/d))%n;//方程ax=b(mod n)的最小解
x=(x%(n/d)+n/d)%(n/d);//方程ax=b(mod n)的最整数小解
printf("%I64d\n",x);
}
}
return ;
}

poj2115[扩展欧几里德]的更多相关文章

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

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

  2. POJ2115 C Looooops 扩展欧几里德

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...

  3. poj2115 Looooops 扩展欧几里德的应用

    好开心又做出一道,看样子做数论一定要先看书,认认真真仔仔细细的看一下各种重要的性质 及其用途,然后第一次接触的题目 边想边看别人的怎么做的,这样做出第一道题目后,后面的题目就完全可以自己思考啦 设要+ ...

  4. poj2142-The Balance(扩展欧几里德算法)

    一,题意: 有两个类型的砝码,质量分别为a,b;现在要求称出质量为d的物品, 要用多少a砝码(x)和多少b砝码(y),使得(x+y)最小.(注意:砝码位置有左右之分). 二,思路: 1,砝码有左右位置 ...

  5. (扩展欧几里德算法)zzuoj 10402: C.机器人

    10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...

  6. [BZOJ1407][NOI2002]Savage(扩展欧几里德)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...

  7. 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm

    欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...

  8. 51nod 1352 扩展欧几里德

    给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数. 提示: 对于第二组测试数据,集合分别 ...

  9. CF 7C. Line(扩展欧几里德)

    题目链接 AC了.经典问题,a*x+b*y+c = 0整数点,有些忘记了扩展欧几里德,复习一下. #include <cstdio> #include <iostream> # ...

随机推荐

  1. crossapp的屏幕适配

    1.分辨率是的某个尺寸大小的屏幕里的像素点数ppi 2.crossapp茶用iphone4为基准比例值为1 3.其它分辨率设备的换算dp = px * 320/ 屏幕PPI 4.crossapp里点. ...

  2. 关于http和https淘宝支付宝跨域解决方法研究

    关于http和http跨域淘宝解决方式研究: http://buyer.trade.taobao.com/trade/pay.htm?spm=a1z01.2.3.4.0.wZAGp9&bizO ...

  3. [转载]Linux 卸载JDK并安装新版本JDK (rpm,tar)

    FROM:http://josh-persistence.iteye.com/blog/1908549 一.查看Jdk的安装路径: whereis javawhich java (java执行路径)e ...

  4. zabbix agent被动模式配置

    zabbix agent检测分为主动(agent active)和被动(agent)两种形式,主动与被动的说法均是相对于agent来讨论的.简单说明一下主动与被动的区别如下: 主动:agent请求se ...

  5. 简易选项卡&&简易JS年历

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. ubuntu apt-get install xxx时一直报错E: Unable to locate package xxxxxxx

    $ sudo add-apt-repository main $ sudo add-apt-repository universe $ sudo add-apt-repository restrict ...

  7. ionic准备之angular基础——模板引入(7)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. centos7 安装docker后启动报错

    启动docker   $ sudo systemctl start docker 报错. 查看状态: $ systemctl status docker.service -l 加 -l 有的行信息很长 ...

  9. Swing中GridBagLayout布局的使用

    1 http://pydoing.blogspot.com/2011/05/java-gridbaglayout.html  台湾人博客,需FQ 2 http://zhangjunhd.blog.51 ...

  10. Codeforces 34C-Page Numbers(set+vector+暴力乱搞)

    C. Page Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...