poj2115[扩展欧几里德]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 22260 | Accepted: 6125 |
Description
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
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 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[扩展欧几里德]的更多相关文章
- C Looooops(poj2115+扩展欧几里德)
C Looooops Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Pr ...
- POJ2115 C Looooops 扩展欧几里德
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...
- poj2115 Looooops 扩展欧几里德的应用
好开心又做出一道,看样子做数论一定要先看书,认认真真仔仔细细的看一下各种重要的性质 及其用途,然后第一次接触的题目 边想边看别人的怎么做的,这样做出第一道题目后,后面的题目就完全可以自己思考啦 设要+ ...
- poj2142-The Balance(扩展欧几里德算法)
一,题意: 有两个类型的砝码,质量分别为a,b;现在要求称出质量为d的物品, 要用多少a砝码(x)和多少b砝码(y),使得(x+y)最小.(注意:砝码位置有左右之分). 二,思路: 1,砝码有左右位置 ...
- (扩展欧几里德算法)zzuoj 10402: C.机器人
10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...
- [BZOJ1407][NOI2002]Savage(扩展欧几里德)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...
- 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm
欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...
- 51nod 1352 扩展欧几里德
给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数. 提示: 对于第二组测试数据,集合分别 ...
- CF 7C. Line(扩展欧几里德)
题目链接 AC了.经典问题,a*x+b*y+c = 0整数点,有些忘记了扩展欧几里德,复习一下. #include <cstdio> #include <iostream> # ...
随机推荐
- django xadmin的使用和改造
django本身自带一个强大的admin后台管理系统,但是管理起来并不是很方便.这里介绍下xadmin,xadmin是基于bootstrap和admin的一个更强大的后台管理系统 github地址ht ...
- github 丢失的本地提交
open git bash git reflog git reset xxxxxxx
- [PWA] Customize the Splash Screen of a PWA built with create-react-app
Android displays a splash screen for PWAs based on the icons and names you provide, but iOS just dis ...
- 接口测试工具(Postman)
给大家介绍一款HTTP接口测试工具 -- Postman ! 优点: 1. 支持参数名与参数值自定义,无论head还是body 2. 支持多种方法调用,包含get与post 3. 支持鉴权加密,包含b ...
- Python线程操作
一.全局锁 1.在Python中,Python代码的执行由Python虚拟机来控制,而在Python虚拟机中,同一时刻只有一个线程在执行,就像单CPU的系统中运行多个进程那样,内存中可以存放多个程序, ...
- Linux非阻塞IO(五)使用poll实现非阻塞的回射服务器客户端
前面几节我们讨论了非阻塞IO的基本概念.Buffer的设计以及非阻塞connect的实现,现在我们使用它们来完成客户端的编写. 我们在http://www.cnblogs.com/inevermore ...
- Google Chrome 35 Released – Install on RHEL/CentOS 6 and Fedora 20-15
Google Chrome is a freeware web browser developed by Google Inc. Google Chrome team proudly announce ...
- Python:Dom生成XML文件(写XML)
http://www.ourunix.org/post/327.html 在python中解析XML文件也有Dom和Sax两种方式,这里先介绍如何是使用Dom解析XML,这一篇文章是Dom生成XML文 ...
- AngularJS---Unknown provider: $routeProvider
AngularJS路由报错: Unknown provider: $routeProvider 根据先知们的指引,在网上爬贴,有翻到官方的解决文章. 原来在AgularJS1.2.0及其之后的版本中, ...
- php 记录图片浏览次数次数
<?php $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; $la ...