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. 响应头里的"Last-Modified"值是怎么来的?

    1.如图所示,app.js文件得到的响应头的"Last-Modified"数值是:Mon, 09 Sep 2013 09:18:22 GMT 我们查看服务器上的app.js文件的修 ...

  2. http://www.cnblogs.com/dolphin0520/p/3949310.html

    http://www.cnblogs.com/dolphin0520/p/3949310.html

  3. 小白学react之网页获取微信用户信息

    通过上一篇<小白学react之EJS模版实战>我们学习了怎样通过EJS模版生成我们高定制化的index.html文件. 本篇我们将会继续延续我们的alt-tutorial项目的实战计划.去 ...

  4. linux的chown命令

    chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...

  5. Android使用TextView,设置onClick属性无效解决的方法

    Android在布局文件里为View提供了onClick属性.用法例如以下: <TextView android:id="@+id/user" android:layout_ ...

  6. 在MyEclipse上安装GIT插件EGit

    MyEclipse2016 CI 下载地址:http://pan.baidu.com/s/1gfBw9Ab 1.“Help”->"Install from Site" 2.在 ...

  7. TCP/IP 网络编程(五)

    优于 select 的 epoll (I/O 复用) select 速度慢的原因 调用select后针对全部文件描写叙述符的循环 每次调用函数时都须要向该函数传递监视对象信息 select并非把发生变 ...

  8. R语言初识

    # 创建数据集&基本数据管理1.向量 创建函数 c() a <- c(1,2,3,4) a[c(i,j)] :[]给定元素所处位置的数值,即向量a中第i和第j个元素,a[2]第二个元素即 ...

  9. docker pull net/http: TLS handshake timeout错误解决

    docker pull  net/http: TLS handshake timeout  出现这个错误,原因很明显,我们在围城里,有两种解决办法,一种是用梯子爬围墙,一种是用国内源,下面用国内源 e ...

  10. php数组操作,内容相同,键值不同,互换

    $title = array("A"=>"创建时间","C"=>"商品信息","D"=& ...