Poj 2115 C Looooops(exgcd变式)
C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22704 Accepted: 6251
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
CTU Open 2004
/*
exgcd变式.
要求:(a+c*x)mod2^k=b.
变形得到:c*xmod2^k=b-a.
即 c*x=(b-a)mod2^k.
用同余方程求解.
(
mod运算是最"自由"的运算
符合常见的运算律.
)
*/
#include<iostream>
#include<cstdio>
#define LL long long
using namespace std;
LL a,b,c,k,x,y;
LL mi(int x)
{
LL tot=1;
for(int i=1;i<=x;i++) tot<<=1;
return tot;
}
LL exgcd(LL a,LL b)
{
if(!b)
{
x=1;y=0;return a;
}
LL d=exgcd(b,a%b);
LL tot=x;
x=y;
y=tot-a/b*y;
return d;
}
int main()
{
int k;
while(scanf("%I64d%I64d%I64d%d",&a,&b,&c,&k)&&a&&b&&c&&k)
{
x=0;y=0;
LL d=exgcd(c,mi(k));
if((b-a)%d) printf("FOREVER\n");
else
{
x=x*(b-a)/d;
LL r=mi(k)/d;
x=(x%r+r)%r;
printf("%I64d\n",x);
}
}
return 0;
}
Poj 2115 C Looooops(exgcd变式)的更多相关文章
- poj 2115 C Looooops——exgcd模板
题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> ...
- 【题解】POJ 2115 C Looooops (Exgcd)
POJ 2115:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A ...
- POJ 2115 C Looooops(扩展欧几里得应用)
题目地址:POJ 2115 水题. . 公式非常好推.最直接的公式就是a+n*c==b+m*2^k.然后能够变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod( ...
- POJ 2115 C Looooops(Exgcd)
[题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...
- POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)
分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...
- POJ 2115 C Looooops(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
- POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))
d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...
- POJ 2115 C Looooops扩展欧几里得
题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #incl ...
- POJ 2115 C Looooops
扩展GCD...一定要(1L<<k),不然k=31是会出错的 .... C Looooops Time Limit: 1000MS Mem ...
随机推荐
- POJ3641-Pseudoprime numbers(快速幂取模)
题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...
- proftpd的示例配置文件
# This is a basic ProFTPD configuration file (rename it to # 'proftpd.conf' for actual use. It estab ...
- openstack 虚拟机流量
- poj 3984 迷宫问题【bfs+路径记录】
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10103 Accepted: 6005 Description ...
- 8-15-Exercise
8-15-小练 这次的题目......只觉得泪奔啊......T T A.HDU 1042 N! 因为0<=n<=1000,故一定要用数组或字符串[同样因为n<=1000故用数组 ...
- 无法通过 128 (在表空间 TEMP 中) 扩展 temp 段
1.查看表空间中数据文件存储的路径以及是否可以自动扩展 SELECT TABLESPACE_NAME, BYTES/1024/1024 FILE_SIZE_MB, FILE_NAME FROM DBA ...
- C#使用参数数组
重载,是指在相同的作用域内,声明多个同名的方法.用以对不同类型或数量的参数的参数执行相同的操作.比如,可以求两个或者三个 int类型数中的最大值,我们可以编写这样的方法实现: class Util { ...
- 11.编写一个Java程序,计算半径为3.0的圆周长和面积并输出结果。把圆周率π定义为常量,半径定义为变量,然后进行计算并输出结果。
package com.hanqi.yzljs; public class yzljs { public static void main(String[] args) { final dou ...
- jsp页面转发到servlet
一个简单的例子来了解一下jsp页面转发到servlet的过程,环境 eclipse.tomcat 1.工程目录结构如下 2.各部分代码如下 1>index.jsp <%@ page lan ...
- Wbemtest查询
运行wbemtest,打开后连接命名空间,默认为“root\cimv2”,可以连接到”IIS管理命名空间(此为Windows Server 2008 R2)“ 查看该命名空间下所有可用的类:单击“枚举 ...