C Looooops(扩展欧几里得求模线性方程)
来源:互联网 发布:各类化验单制作软件 编辑:程序博客网 时间:2024/04/30 03:51
Link:http://poj.org/problem?id=2115
C Looooops
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20167 Accepted: 5416
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
Source
CTU Open 2004
AC code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <vector>#include <string>#include <queue>#include <stack>#include <algorithm>#define PI acos(-1.0)#define LINF 1000000000000000000LL#define eps 1e-8#define LL long long#define MAXN 100010 #define MOD 1000000007using namespace std;const int INF=0x3f3f3f3f;LL exgcd(LL A,LL &x,LL B,LL &y){ LL x1,y1,x0,y0; x0=1;y0=0; x1=0;y1=1; LL r=(A%B+B)%B; LL q=(A-r)/B; x=0;y=1; while(r) { x=x0-q*x1; y=y0-q*y1; x0=x1; y0=y1; x1=x;y1=y; A=B;B=r;r=A%B; q=(A-r)/B; } return B;}LL ABS(LL x){return x>=0?x:-x;}//求ax=b(mod n)等价于ax+ny=b,故可以这样转化过来 int main(){ long long A,B,C,k,x,y; while(scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)!=EOF) { if(!A&&!B&&!C&&!k)break; long long a,b,n,d; a=C; b=B-A; n=(long long)1<<k;d=exgcd(a,x,n,y); //求a,n的最大公约数d=gcd(a,n)和方程d=ax+by的系数x、y if(b%d)printf("FOREVER\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); } /* 不知为何,下面的输出会RE!!! /*LL x0,y0,flag=0; if(a<b){flag=a;a=b;b=flag;flag=1;}//默认令a>=b,flag=1说明有交换,则对应的系数x和y最后输出时也要交换后输出 LL d=exgcd(a,x0,b,y0);//返回a、b的最大公约数 //求ax+by=c if(c%d!=0)//无解 { printf("FOREVER\n");}else{x0=x0*(c/d);//x的一个解 y0=y0*(c/d);//y的一个解 LL t=y0/(a/d),minx=LINF,miny=LINF,minn=LINF; for(int i=t-1;i<=t+1;i++) { //x的所有解是x=x0+b/d*t,y的所有解是:y=y-a/d*t。 x=x0+b/d*i; y=y0-a/d*i; //printf("%d %d\n",x,y);//打印所有解 /* if(ABS(x)+ABS(y)<minn) { minn=ABS(x)+ABS(y); minx=ABS(x); miny=ABS(y); }*/ /* if(x>=0) { minx=min(minx,x);} if(y>=0) { miny=min(miny,y);} } if(flag)printf("%lld\n",miny); else printf("%lld\n",minx); /* if(flag)printf("%lld\n",miny); else printf("%lld\n",minx);*/ //printf("%lld\n",minx); //} } return 0;}
0 0
- C Looooops(扩展欧几里得求模线性方程)
- C Looooops poj2115 (扩展欧几里得+模线性方程)
- POJ 2115 C Looooops 模线性方程(扩展欧几里得)
- POJ 2115 C Looooops 解模线性方程(扩展欧几里得)
- poj_2115 C Looooops(模线性方程+扩展欧几里得)
- [ACM] POJ 2115 C Looooops (扩展欧几里得求解模线性方程)
- POJ 2115 C Looooops(扩展欧几里德 + 求解模线性方程)
- POJ2115 C Looooops ——模线性方程(扩展gcd)
- C Looooops--模线性方程
- C Looooops(扩展欧几里得)
- 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(扩展欧几里得)
- java进行多张图片组合一张图片
- Atitit.使用引擎加脚本架构的设计 使用php,js来开发桌面程序。。
- Activity 启动模式以及Inent Flags理解
- linux Web服务程序监控shell脚本
- [leetcode] 99.Recover Binary Search Tree
- C Looooops(扩展欧几里得求模线性方程)
- EdtText默认显示数字键盘,不强制输入数字
- python学习笔记
- 各类排序算法性能对比简述
- atitit.jquery tmpl模板总结 .doc
- 使用 Sahi 实现 Web 自动化测试
- sscanf
- Android Toolbar样式定制详解
- UIPickView