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; 题目要求我们在有解的情况下求出最小的解,我们转 ...
随机推荐
- CAF(C++ actor framework)(序列化之复杂类,分析 还有自己不懂的细思恐极函数实现)(三)
这里应该是序列化的最后一篇.感觉自己写的不是很好,也一点点在学习.这次就不贴上代码了.代码在github上的announce5.cpp.代码简单,但是分析下去会有细思恐极的感觉! 先看一下几个函数是干 ...
- yum的一些用法
对于配置仓库这里就不做讲解了,这里只是列出比较实用的yum的用法 yum install packagename #安装软件包 yum remove packagena ...
- DTCMS更改图片相册上传图片类型,手机上传图片相册
/admin/js/uploader.js 中 filetypes: "jpg,jpge,png,gif", //文件类型 改为 filetypes: "jpg,jpeg ...
- mouseenter 事件,固定右侧客服特效
不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. 当鼠标指针离开元素时,会发生 mouseleave 事件 ...
- Apache+PHP+MySql 的安装及配置
每一项技术用的人多了,就会有人将其进行优化,做成一个简单.实用.大众化的工具,这对于初识者来说是非常方便的,但是对于长久学习或工作这方面的人技术人员来说是不可取的,所以还是要学习基础的实用方法.因此, ...
- 《WPF程序设计指南》读书笔记——第3章 内容的概念
1.Content属性及字体相关的属性 using System; using System.Windows; using System.Windows.Media; namespace LY.Dis ...
- Pop Sequence (栈)
Pop Sequence (栈) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, ...
- Cube and EarthDistance
前言:项目中用到了postgreSQL中的earthdistance()函数功能计算地球上两点之间的距离,中文的资料太少了,我找到了一篇英文的.讲的很好的文章 ,特此翻译,希望能够帮助到以后用到ear ...
- 备份了一个nginx的虚拟主机配置文件报错
[root@localhost vhost]# service nginx restart 停止 nginx:[确定] 正在启动 nginx:nginx: [warn] conflicting ser ...
- 微软职位内部推荐-Senior NLP Scientist & Developer
微软近期Open的职位: Contact Person: Winnie Wei (wiwe@microsoft.com )Senior Software Development Engineer/NL ...