POJ2115——C Looooops(扩展欧几里德+求解模线性方程)
C Looooops
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
题目大意:
对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次才会结束。
若在有限次内结束,则输出循环次数。
否则输出死循环。
解题思路:
有题意可知:设进行X次循环 A%2^K+(C*X)%2^K=B%2^K
-->(C*X)%(2^K)=(B-A)%(2^K)
-->模线性方程 ax=b mod n的解。 其中a=C,b=B-A,n=2^K.
算法:
算法导论上给出了 求解a=b (mod n)的伪代码 其中a,n为正整数,b为任意整数。
MODULAR_LINEAR_EQUATION_SOLVER(a,b,n)
(d,x',y')=EXTENDED_EUCLID(a,n)
if (d|b)
x0=x'(b/d) mod n
for i=0 to d-1
print (x0+i(n/d)) mod n
else
print "no solutions"
其中x0+i(n/d)为所有可以的解,输出其最小正整数解即可。
Code:
/*************************************************************************
> File Name: poj2115.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月29日 星期三 13时13分46秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 100000
#define LL long long
using namespace std;
LL extended_gcd(LL a,LL b,LL &x,LL &y)
{
LL ret,tmp;
if (b==)
{
x=,y=;
return a;
}
ret=extended_gcd(b,a%b,x,y);
tmp=x;
x=y;
y=tmp-a/b*y;
return ret;
} int main()
{
LL A,B,C,k;
while (cin>>A>>B>>C>>k)
{
if (A==&&B==&&C==&&k==) break;
LL mod=;
while (k--)
mod*=;
LL x,y;
LL a=C,b=B-A;
LL d=extended_gcd(a,mod,x,y);
//cout<<a<<" "<<x<<" "<<mod<<" "<<y<<endl;
//cout<<d<<endl;
if (b/d*d==b)
{
LL x0=x*(b/d)%mod;
if(x0<) x0+=mod;
//cout<<x0<<endl;
long long Min=x0;
for (int i=;i<=d-;i++)
if ((x0+i*(mod/d))%mod>=)
Min=min(Min,(x0+i*(mod/d))%mod);
cout<<Min<<endl;
}
else
printf("FOREVER\n");
}
return ;
}
POJ2115——C Looooops(扩展欧几里德+求解模线性方程)的更多相关文章
- POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))
d.对于这个循环, for (variable = A; variable != B; variable += C) statement; 给出A,B,C,求在k位存储系统下的循环次数. 例如k=4时 ...
- POJ2115 C Looooops 扩展欧几里德
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2115 题意 对于C的for(i=A ; i!=B ;i +=C)循环语句,问在k位存储系统中循环几次 ...
- poj2115 Looooops 扩展欧几里德的应用
好开心又做出一道,看样子做数论一定要先看书,认认真真仔仔细细的看一下各种重要的性质 及其用途,然后第一次接触的题目 边想边看别人的怎么做的,这样做出第一道题目后,后面的题目就完全可以自己思考啦 设要+ ...
- poj2115-C Looooops(扩展欧几里德算法)
本题和poj1061青蛙问题同属一类,都运用到扩展欧几里德算法,可以参考poj1061,解题思路步骤基本都一样.一,题意: 对于for(i=A ; i!=B ;i+=C)循环语句,问在k位存储系统中循 ...
- poj 2115 C Looooops 扩展欧几里德
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23616 Accepted: 6517 Descr ...
- POJ Widget Factory 【求解模线性方程】
传送门:http://poj.org/problem?id=2947 Widget Factory Time Limit: 7000MS Memory Limit: 65536K Total Su ...
- POJ2115 C Looooops[扩展欧几里得]
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24355 Accepted: 6788 Descr ...
- C Looooops(扩展欧几里德)
C Looooops Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)
分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...
随机推荐
- 加密解密以及CA签证
在当今互联网时代,数据越来越来重要.那么如何加密?解密?以及通过什么方式来认证了?? 接下来,我就会和大家谈谈加密,解密以及CA签证的实现. 首先大家的知道一些加密,解密的基本常识: 互联网上中间人一 ...
- ASP.NET 发送电子邮件简介
1.补充知识 (1)POP3和SMTP服务器是什么? 简单点来说:POP3 用于接收电子邮件 ,SMTP 用于发送电子邮件. (1)POP3具体指什么? POP3(Post Office Protoc ...
- 《C和指针》 读书笔记 -- 第9章 字符串、字符和字节
1.字符串以NUL结尾,但字符串长度不包括NUl字节. 2.复制字符串 char *strcpy(char *dst,char const *src); 3.连接字符串 char *strcat(ch ...
- Oracle中正则表达式的使用
Oracle10开始支持正则表达式. ORACLE中的支持正则表达式的函数主要有下面四个: 1. REGEXP_LIKE : 与LIKE的功能相似 2. REGEXP_INSTR : ...
- Nginx upstream的5种权重分配方式
.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,后端服务器down掉,能自动剔除 .weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况. upstre ...
- To change the sharepoint CA port
Set-SPCentralAdministration -Port <port number> to fix the error: Got this error: Failed to re ...
- 在cmd命令行下登录本地oracle数据库与服务器上的oracle
一.本地登录 cmd->sqlplus 用户名/密码@数据库名 (as sysdba)->enter注:如果此用户名不是系统用户sys,scott,system就不需要加as sysdba ...
- WebView重定向新开界面问题-b
首先介绍下这个问题,iOS上WebView 如果想更贴近native,就要加载新URL的时候新开个界面,但是如果加载的链接有重定向的话,就会在中间开一个空白的界面,这个好烦.然后就是解决这个问题,采用 ...
- 对.net orm工具Dapper在多数据库方面的优化
Dapper是近2年异军突起的新ORM工具,它有ado.net般的高性能又有反射映射实体的灵活性,非常适合喜欢原生sql的程序员使用,而且它源码很小,十分轻便.我写本博客的目的不是为了介绍Dapper ...
- ExtJS4.2学习(18)时间控件(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-12-22/190.html 感谢“束洋洋 ”的付出. 前言 ...