[暑假集训--数论]poj2115 C Looooops
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.
Input
The input is finished by a line containing four zeros.
Output
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
给a,b,c,k,求最小的x,使得a+c*x==b%(2^k)
即c*x+(2^k)*y==(b-a+2^k)%(2^k)
直接上exgcd,然后调一调x得到最小解即可
#include<cstdio>
#include<iostream>
#include<cstring>
#define LL long long
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL A,B,C,D,k;
inline LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(!b){x=;y=;return a;}
LL gcd=exgcd(b,a%b,x,y);
LL t=x;x=y;y=t-a/b*y;
return gcd;
}
inline LL calc(LL a,LL b,LL c)//Ax==B(mod C)
{
LL x,y;
LL tt=exgcd(a,c,x,y);
if (b%tt!=)return -;
x=x*b/tt;
LL ss=c/tt;
x=(x%ss+ss)%ss;
return x;
}
int main()
{
while (~scanf("%lld%lld%lld%lld",&A,&B,&C,&k)&&(A+B+C+k))
{
D=1ll<<k;
if (B==A){puts("");continue;}
B=(B-A+D)%D;
LL ans=calc(C,B,D);
if (ans==-)puts("FOREVER");
else printf("%lld\n",ans);
}
}
poj 2115
[暑假集训--数论]poj2115 C Looooops的更多相关文章
- [暑假集训--数论]hdu2136 Largest prime factor
Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...
- [暑假集训--数论]hdu1019 Least Common Multiple
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...
- [暑假集训--数论]poj1365 Prime Land
Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...
- [暑假集训--数论]poj2034 Anti-prime Sequences
Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement ...
- [暑假集训--数论]poj1595 Prime Cuts
A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...
- [暑假集训--数论]poj2262 Goldbach's Conjecture
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...
- [暑假集训--数论]poj2909 Goldbach's Conjecture
For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...
- [暑假集训--数论]poj3518 Prime Gap
The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...
- [暑假集训--数论]poj1730 Perfect Pth Powers
We say that x is a perfect square if, for some integer b, x = b 2. Similarly, x is a perfect cube if ...
随机推荐
- vue动画使用javascript钩子函数
钩子函数从before-enter – enter –after-enter-entercancelled也是一个完整的生命周期 <transition v-on:before-enter= ...
- Log错误日志级别
日志记录器(Logger)的级别顺序: 分为OFF.FATAL.ERROR.WARN.INFO.DEBUG.ALL或者您定义的级别.Log4j建议只使用四个级别,优先级 从高到低分别是 ERR ...
- css分层,实现遮罩底层弹出新窗口里可以操作,最下层能看到单不能操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ansible指令和常用模块使用
这里文章记录一下ansible的指令选项和常用的模块使用 ansible指令选项 -m:要执行的模块,默认为command -a:模块的参数 -u:ssh连接的用户名,默认用root,ansible. ...
- pycharm 语言配置
在pycharm 安装所在位置找到 lib 文件夹 打开后找到 rescources_**.jar 文件 **为语言类型,英语为en 中文为cn, 用相应语言文件替换,便可变成相应语言 https:/ ...
- 三、Linux 系统目录结构
Linux 系统目录结构 登录系统后,在当前命令窗口下输入命令: ls / 你会看到如下图所示: 树状目录结构: 以下是对这些目录的解释: /bin:bin是Binary的缩写, 这个目录存放着最 ...
- 二十八、MySQL 元数据
MySQL 元数据 你可能想知道MySQL以下三种信息: 查询结果信息: SELECT, UPDATE 或 DELETE语句影响的记录数. 数据库和数据表的信息: 包含了数据库及数据表的结构信息. M ...
- 判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备)
判断浏览器环境(QQ,微信,安卓设备,IOS设备,PC微信环境,移动设备) // ===== 判断浏览器环境 ===== // // 判断是否是QQ环境 function isQQ() { retur ...
- Thinkphp5中的Validate验证器的使用
更多笔记: http://note.youdao.com/noteshare?id=e97a5df64888f27d912b3e966b9ec297&sub=web1520841813815 ...
- 自动化运维之使用Python3收发电子邮件~~~附源码
一.背景介绍 1.1 一些专业名称的解释 MUA——Mail User Agent,邮件用户代理.是用户与电子邮件系统的交互接口,一般来说它就是我们PC机上的一个程序,提供一个好的用户界面,它提 ...