[暑假集训--数论]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 ...
随机推荐
- 在主机端和设备端进行”incrementArray“并对结果进行比较
实验思想: 在主机端将数据初始化后传输到设备端,设备端和主机端进行同样的操作对数据加1,然后将设备端的结果传输到主机,最后核对主机端的计算结果和设备端的计算结果是否一直. // incrementAr ...
- java基础编程——获取栈中的最小元素
题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 题目代码 /** * Created by YuKai Fan on 2018/9 ...
- javase(9)_java io系统
一.File类 1.file既可以代表一个特定文件的名称,又可以代表一个目录下的一组文件的名称,实际上,FilePath对这个类来说是个更好的名字.2.目录列表器例: import java.io.F ...
- VueX源码分析(5)
VueX源码分析(5) 最终也是最重要的store.js,该文件主要涉及的内容如下: Store类 genericSubscribe函数 resetStore函数 resetStoreVM函数 ins ...
- Spring Cloud学习介绍
最近在学spring cloud, 整理了下 简单知识要求: 1.要了解springboot 2.了解分布式架构 3.了解微服务 4.了解springcloud是做什么的 带着这些,初学者 就至少有个 ...
- 在生产环境下实现每天自动备份mysql数据库
1.描述 我相信很多朋友在工作都都会有这种需求,老板或领导让你每天都要备份mysql数据库,你该如何实现呢,是每天到一定的时间在服务器上敲一遍mysql的备份命令,还是想写个脚本,定时定点的自动备份呢 ...
- 利用DOM的方式点击切换图片及修改文字
本案例主要学习理解,用到的几个DOM方法 01.getAttribute()方法,获取元素的属性值 02.setAttribute('src',source) 方法,用后边的值修改前边这个元素的属性值 ...
- JZOJ 5456. 【NOIP2017提高A组冲刺11.6】奇怪的队列
5456. [NOIP2017提高A组冲刺11.6]奇怪的队列 (File IO): input:queue.in output:queue.out Time Limits: 1000 ms Mem ...
- VMware之无法切换桥接网络
一.关闭正在运行的虚拟机 二.打开虚拟网络编辑器 三.还原默认设置 四.启动虚拟机即可正常使用桥接网络
- ajax跨域请求的处理
跨域的情形有很多种,网上有人给出了一份表格, 表格中标识为”不允许”通信的情况都属于跨域.实际网络服务中需要跨域的情况确实存在,于是开发者们提供了一种解决方案,就是使用jsonp格式进行数据交互,它不 ...