POJ-2115-C Looooops(线性同余方程)
链接:
https://vjudge.net/problem/POJ-2115
题意:
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 < 2 k) modulo 2 k.
思路:
求解\(a+c*x \equiv b (mod\,2^k)\).
转化\(c*x + 2^k = b-a\).
扩展欧几里得求解.
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 1e6+10;
LL a, b, c, k;
LL ExGcd(LL a, LL b, LL &x, LL &y)
{
if (b == 0)
{
x = 1, y = 0;
return a;
}
LL d = ExGcd(b, a%b, x, y);
LL tmp = x;
x = y;
y = tmp - (a/b)*y;
return d;
}
int main()
{
while(~scanf("%lld%lld%lld%lld\n", &a, &b, &c, &k) && (a || b || c || k))
{
LL x, y;
LL B = 1LL<<k;
LL d = ExGcd(c, B, x, y);
if ((b-a)%d != 0)
{
puts("FOREVER");
continue;
}
x = x*(b-a)/d;
x = (x%(B/d)+(B/d))%(B/d);
printf("%lld\n", x);
}
return 0;
}
POJ-2115-C Looooops(线性同余方程)的更多相关文章
- POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)
分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...
- 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(扩展欧几里得应用)
题目地址:POJ 2115 水题. . 公式非常好推.最直接的公式就是a+n*c==b+m*2^k.然后能够变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod( ...
- poj2115-C Looooops -线性同余方程
线性同余方程的模板题.和青蛙的约会一样. #include <cstdio> #include <cstring> #define LL long long using nam ...
- 【题解】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【数论】
很容易看出来一个同余式,说到底是解一个线性同余方程,计算机解通常有拓展欧几里得和欧拉定理两种算法,参照去年的NOIP水题,问题是这题数据范围是2^32所以要int64 TAT #include< ...
- Poj 2115 C Looooops(exgcd变式)
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22704 Accepted: 6251 Descripti ...
- POJ 2115 C Looooops(模线性方程)
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思 ...
- POJ 2115 C Looooops(Exgcd)
[题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, ...
随机推荐
- 修改主机名和修改主机映射和ssh免登陆
1.修改主机名 vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=cc3 2.修改主机映射 vi /etc/hosts 127.0.0.1 loca ...
- c++中继承的使用
1.c++中继承有公有继承,保护继承,私有继承 定义个基类: #include using namespace std; class Base { public: void display() { c ...
- 下载安装Git,学习笔记
官方地址为:https://git-scm.com/download/win 2.下载完之后,双击安装,全部选择默认. 3.选择安装目录 4.选择组件 5.开始菜单目录名设置 6.选择使用命令行环境 ...
- Apache Rewrite 规则详解知识大全
Rewrite是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术.它还可以实现限制特定IP访问网站的功能. 1.Rewrite标志 R[=code](for ...
- firefox 获取xpath
在做一个爬虫是,输入内容后,会自动显示内容,而且只能选择,不能根据输入的提交,一点就失去焦点,找不到相关内容 后来发现firefox的查看元素的最左边的类似于鼠标尖头的按钮,就是确保这种情况下,去查找 ...
- Model 的使用
1. 设计数据结构 问题表Question:作用存放问题 id 主键 自增 question_text 题目 varchar120 created 创建时间 datetime 选项表Choice: ...
- WUSTOJ 1307: 校门外的树(Java)
题目链接:
- Python3遍历指定文件夹下所有文件及文件夹
采用os模块儿: import os def get_filelist(dir): for home, dirs, files in os.walk(dir): print("####### ...
- 5-24 c++语言之【基础知识】
最近一段时间继续开始了c++的学习,作为c plus plus 难免会与c语言做一个对比,很明显的感受到c++语言注重代码的复用性和拓展性,而c语言更加注重其算法的高效性,这也是今后需要注意的地方,避 ...
- js实现CheckBox全选或者不全选
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">< ...