poj 2115 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 <= 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 ( <= k <= ) is the number of bits of the control variable of the loop and A, B, C ( <= 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
Sample Output
FOREVER
Source
解题思路:这道题和POJ1061(青蛙约会)一样,都是同余方程的求解,用到了拓展欧几里德算法。而本题题意明确,就是求解这个公式:(a+c*x)mod2^k=b ,求得x 的最小解。变形后可得:c*xmod2^k=b-a,即 c*x=(b-a)mod2^k; 这就是标准的同余方程。
注意:k <=32 ,而 2的 32次方超出整数范围,所以要用__int64或long long ,就不会出现runtime error了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1000000
#define inf 1e12
ll fac(ll m){
ll ans=;
for(ll i=;i<m;i++){
ans=ans*;
}
return ans;
}
ll e_gcd(ll a,ll b,ll &x,ll &y){
if(b==)
{
x=;
y=;
return a;
}
ll r=e_gcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-a/b*y;
return r;
}
int main()
{
ll a,b,c,k;
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k)==){
if(a== && b== && c== && k==){
break;
}
ll x,y,r;
ll d=e_gcd(c,fac(k),x,y);
//printf("---%I64d %I64d %I64d\n",d,x,y);
if((b-a)%d!=){
printf("FOREVER\n");
}
else{
x=x*(b-a)/d;
r=fac(k)/d;
x=(x%r+r)%r;
printf("%I64d\n",x);
}
}
return ;
}
poj 2115 C Looooops(推公式+扩展欧几里得模板)的更多相关文章
- [POJ 2115} C Looooops 题解(扩展欧几里德)
题目描述 对于C的for(i=A ; i!=B ;i +=C)循环语句,给出A,B,C和k(k表示变量是在k进制下的无符号整数),判断循环次数,不能终止输出"FOREVER". 输 ...
- poj 1061 青蛙的约会 (扩展欧几里得模板)
青蛙的约会 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status ...
- POJ - 1061 青蛙的约会 (扩展欧几里得求同余式)
题意:两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对 ...
- Uva12169 扩展欧几里得模板
Uva12169(扩展欧几里得) 题意: 已知 $x_i=(a*x_{i-1}+b) mod 10001$,且告诉你 $x_1,x_3.........x_{2t-1}$, 让你求出其偶数列 解法: ...
- 扩展欧几里得模板&逆元求法
拓展欧几里得: 当 gcd ( a , b )= d 时,求绝对值和最小的 x , y 使得 x * a + y * b = d : d = gcd ( a , b ) = gcd ( b , a m ...
- POJ 2115 C Looooops( 简单拓欧 + 快速幂 )
链接:传送门 题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死 ...
- POJ 1061 青蛙的约会(扩展欧几里得)
根据题意,两个青蛙跳到同一个点上才算是遇到了,所以有 (x+m*t) - (y+n*t) = p * ll; (t是跳的次数,ll是a青蛙跳的圈数跟b青蛙的圈数之差.整个就是路程差等于纬度线周长的整 ...
- POJ 1061 青蛙的约会 扩展欧几里得
扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...
- POJ1061 青蛙的约会(扩展欧几里得)
题目链接:http://poj.org/problem?id=1061 青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
随机推荐
- Android基础之退出应用程序Demo
对于Android我也不是很熟悉,只是学习一些基本内容就OK.所以写的内容也很简单.本Demo要实现的效果就是双击返回键弹出提示框确认是否退出程序. 一.废话少说直接上代码.至于涉及到的相关包在Ecl ...
- WPF弹出对话确认框
MessageBoxResult mr = CMessageBox.ShowQuestionMessage("点击“是”继续,点击“否”取消.", "确认删除?" ...
- Xcode5和6上新建工程如何本地化启动页面
建议阅读本篇文章前先具备iOS本地化的基本知识,Google中搜索“iOS本地化”,有成片的教程~~ 最近有个app需要支持英语.简体中文.繁体中文,由于启动页面上有文字,所以也不得不做下本地化处理. ...
- SQLServer 跨服务器查询的两个办法
网上搜了跨服务器查询的办法,大概就是Linked Server(预存连接方式并保证连接能力)和OpenDataSource(写在语句中,可移植性强).根据使用函数的不同,性能差别显而易见...虽然很简 ...
- 前端--关于客户端javascript
浏览器中的Javascript 客户端javascript就是运行在浏览器中的javascript,现代的浏览器已经有了很好的发展,虽然它是一个应用程序,但完全可以把它看作是一个简易的操作系统,因为像 ...
- 企业qq代码,工作中用到的
<div id="xixi" onmouseover="toBig()" style="top: 120px; left: 0; positio ...
- css链接,列表,表格
1.css链接 a:link - 正常,未访问过的链接 a:visited - 用户已访问过的链接 a:hover - 当用户鼠标放在链接上时 a:active - 链接被点击的那一刻 注意: a:h ...
- swift——设置navigationitemtitle的内容以及格颜色
1.用UILabel,自定义整个titleview // var TitleText = UILabel() self.TitleText.frame = CGRectMake(0, 0, 100, ...
- 自定义tableviewCell的分割线
第一种:addsubview UIView *line = [[UIView alloc]initWithFrame:CGRectMake(10, cellH-0.5, DEVW-10, 0.5)]; ...
- 省市联动JQ封装比较简洁调用的方法
前言 因为省市联动的需求在每个项目几乎存在,所以本人也对此在web页面通过封装比较简洁的JQ方法循环判断调用调用后台获取数据再绑定到Select表单上.如果对代码有什么疑问或者更好办法可以在评论区留言 ...