poj 2115 C Looooops【数论】【欧几里得算法】
来源:互联网 发布:绘制流程图的软件 编辑:程序博客网 时间:2024/05/19 23:16
C Looooops
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 17556 Accepted: 4540
Description
A Compiler Mystery: We are given a C-language style for loop of type
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.
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.
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 163 7 2 167 3 2 163 4 2 160 0 0 0
Sample Output
0232766FOREVER
题目大意:
给你ABCk四个数字,i=A开始 i+=C为每轮加的值,i==B为停止的条件,以2^k作为存储单位。
即方程: x=[(B-A+2^k)%2^k] /C
求x的值,讲A-B化为b 2 ^k化为n C化为a,则原方程化为 ax=b(mod n)
也就是求解模线性方程了,用的是扩展欧几里得算法........
一点点知识点花了好久的时间,总算是有所理解了。
下面是扩展欧几里得算法求不定方程ax+by=c的值:
bool linear_equation(LL a,LL b,LL c,LL &x,LL &y){ LL d=ex_gcd(a,b,x,y); if(c%d!=0) //即不整除 return false; LL k=c/d; x*=k; y*=k; //求得的只是其中一组解 return true;}扩展欧几里得算法可以求出ax+by=gcd(a,b) 的值,这个值记为d,如果说c的值不是d的值的倍数,则一定无解,否则乘起来就得到答案了
注意:求得的x0,y0只是一组特解,另外的解按照这个规则: x=x0+b/gcd(a,b) y=y0-a/gcd(a,b)来算得
而模线性方程则有点麻烦:求解 ax=b(mod n)
化为ax+ny=b的x的解,首先我们先把a和n的最大公约数d求出来,然后一样的道理如果n不是d的倍数则一定无解
下面就是求出一组特解x0=x*(b/d)%n; 为什么与之前的x的特解不一样要在后面mod一个n是因为这是一个线性同余方程
bool modular_linear_equation(LL a,LL b,LL n){ LL x,y,x0,i; LL d=ex_gcd(a,n,x,y); if(b%d!=0) return false; x0=x*(b/d)%n; //特解 for(i=1;i<d;i++) //解的个数是d printf("%d\n",(x0+i*(n/d))%n); return true;}
下面附:欧几里得算法的模板......
#include<iostream>#include<stdio.h>using namespace std;#define LL long longLL gcd(LL a,LL b){ if(b==0) return a; else return gcd(b,a%b);}LL ex_gcd(LL a,LL b,LL &x,LL &y){ if(b==0) { x=1;y=0; return a; } LL r=ex_gcd(b,a%b,x,y); LL t=x; x=y; y=t-a/b*y; return r;}//求解ax+by=c的x,y的解bool linear_equation(LL a,LL b,LL c,LL &x,LL &y){ LL d=ex_gcd(a,b,x,y); if(c%d!=0) //即不整除 return false; LL k=c/d; x*=k; y*=k; //求得的只是其中一组解 return true;}bool modular_linear_equation(LL a,LL b,LL n){ LL x,y,x0,i; LL d=ex_gcd(a,n,x,y); if(b%d!=0) return false; x0=x*(b/d)%n; //特解 for(i=1;i<d;i++) //解的个数是d printf("%d\n",(x0+i*(n/d))%n); return true;}
下面附这道题目的主函数:
int main (){ LL A,B,C,k; while(~scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)&&(A||B||C||k)) { LL a=C; LL b=B-A; LL n=1LL<<k; LL x,y; LL d=ex_gcd(a,n,x,y); if(b%d!=0) cout<<"FOREVER"<<endl; else { x=(x*(b/d))%n; //特解 x=(x%(n/d)+n/d)%(n/d); //方程ax=b(mod n)的最整数小解 printf("%I64d\n",x); } }}注意点就是定义1<<k的时候要这样定义 1LL<<k 这样定义出来才是long long 的类型
0 0
- poj 2115 C Looooops【数论】【欧几里得算法】
- [数论]POJ 2115/HOJ 1787 C Looooops 扩展欧几里得算法
- POJ 2115 C Looooops 扩展欧几里得算法
- poj 2115 C Looooops (扩展欧几里得算法)
- poj 2115 C Looooops 扩展欧几里得算法
- POJ 2115 C Looooops (扩展欧几里得算法)
- POJ 2115 C Looooops <扩展欧几里得算法>
- POJ 2115 C Looooops [扩展欧几里得]【数论】[水]
- 【数论】 POJ 2115 C Looooops
- poj 2115 C Looooops 数论
- (模板题)poj 2115 C Looooops(扩展欧几里得算法)
- POJ 2115 C Looooops——拓展欧几里得算法
- 20140714 「初等数论 - 拓展欧几里得+同余模方程」POJ 2115 C Looooops
- POJ 2115 C Looooops(扩展欧几里得)
- poj 2115 C Looooops(扩展欧几里得)
- poj-2115 C Looooops(扩展欧几里得)
- poj 2115 C Looooops (扩展欧几里得)
- poj 2115 C Looooops 扩展欧几里得
- 选择开始时间,结束时间自动加一年或某个时间。
- Netty In Action中文版 - 第十二章:SPDY
- Ganglia API安装与使用
- 数据挖掘十大经典算法(8) kNN: k-nearest neighbor classification
- 103_slidingdrawer控件的使用
- poj 2115 C Looooops【数论】【欧几里得算法】
- Android截图
- Android Launcher分析和修改12——Widget列表信息收集
- hdu 1023 Train Problem II
- hdu 2049 不容易系列之(4)——考新郎(错排+排列组合)
- 关于UIp通讯自己主动发送的办法
- 好文收录
- Java基础温习 (8)Java内部类的使用小结
- view & viewGroup 调用顺序