Looooops(求解同余方程、同余方程用法)【拓展欧几里得】
Looooops(点击)
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.
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 < 2 k) 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
思路:
题目搁了两天,开始想了个很复杂的方法,花了好长时间调试结果TLE
后来实在没办法去搜了一下,才知道是用同余方程 解决
根据题目可以列出一个方程式,将加c的次数设成x:
∵ a+cx≡b(%mod);
∴ a+c*x+mod*y=b;
∴ c*x+mod*y=b-a;
根据exgcd可以求解 同时好要求出最小整数x的解就是结果
代码:
#include<stdio.h>
typedef long long LL;
LL GCD;
LL exgcd(LL a,LL b,LL &x,LL &y) // 拓展欧几里得求x、y特解
{
if(!b){
x=1;y=0;return a;
}
GCD=exgcd(b,a%b,y,x);
y-=a/b*x;
return GCD;
}
LL qpow(LL c,LL q) // 快速幂 因为题目涉及求2^k 如果用pow可能会出错
{
LL ans=1; //快速幂里面不需要%mod 和mod没有关系 不需要担心快速幂结果会超过2^k
while(q){
if(q%2){
ans*=c;
}
c*=c;
q/=2;
}
return ans;
}
int main()
{
LL a,b,c,k,x,y,t;
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k)!=EOF){
if(a==0&&b==0&&c==0&&k==0){
break;
}
else{
GCD=exgcd(c,qpow(2,k),x,y); //将 c、mod=2^k、x、y 依次带入拓展欧几里得方程求解
if((b-a)%GCD){
printf("FOREVER\n"); // 判断方程是否有解
}
else{
x*=((b-a)/GCD); //类似求解ax+by=c最小整数解的方法 得出最小x的值
t=qpow(2,k)/GCD; //因为求x所以t=b/GCD 但这个b并不是输入的b 而是 方程里面对应
if(t<0){ 的b 即 qpow(2,k)
t=-t;
}
x=(x%t+t)%t;
printf("%lld\n",x); //输出结果x
}
}
}
return 0;
}
Looooops(求解同余方程、同余方程用法)【拓展欧几里得】的更多相关文章
- 【lydsy1407】拓展欧几里得求解不定方程+同余方程
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1407 题意: 有n个野人,野人各自住在第c[i]个山洞中(山洞成环状),每年向前走p[i] ...
- [POJ2115]C Looooops 拓展欧几里得
原题入口 这个题要找到本身的模型就行了 a+c*x=b(mod 2k) -> c*x+2k*y=b-a 求这个方程对于x,y有没有整数解. 这个只要学过拓展欧几里得(好像有的叫扩展欧几里德QA ...
- ACM数论-欧几里得与拓展欧几里得
ACM数论——欧几里得与拓展欧几里得 欧几里得算法: 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd ...
- Vulnerable Kerbals CodeForces - 772C【拓展欧几里得建图+DAG上求最长路】
根据拓展欧几里得对于同余方程 $ax+by=c$ ,有解的条件是 $(a,b)|c$. 那么对于构造的序列的数,前一个数 $a$ 和后一个数 $b$ ,应该满足 $a*x=b(mod m)$ 即 $ ...
- HDU-3579-Hello Kiki (利用拓展欧几里得求同余方程组)
设 ans 为满足前 n - 1个同余方程的解,lcm是前n - 1个同余方程模的最小公倍数,求前n个同余方程组的解的过程如下: ①设lcm * x + ans为前n个同余方程组的解,lcm * x ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- BZOJ-1407 Savage 枚举+拓展欧几里得(+中国剩余定理??)
zky学长实力ACM赛制测试,和 大新闻(YveH) 和 华莱士(hjxcpg) 组队...2h 10T,开始 分工我搞A,大新闻B,华莱士C,于是开搞: 然而第一题巨鬼畜,想了40min发现似乎不可 ...
- [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)
Power of Fibonacci Time Limit: 5 Seconds Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...
- 51 Nod 1256 乘法逆元(数论:拓展欧几里得)
1256 乘法逆元 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K ...
随机推荐
- Pyqt5_QLabel
QLabel 作用 方法 信号 作用 占位符.显示文本.显示图片.放置gif动画.超链接.提示标记 方法 setAlignment() 按固定值方式对齐文本 Qt.AlignLeft:水平方向靠左对齐 ...
- Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(一)
好吧,这个题目我也想了很久,不知道如何用最简单的几个字来概括这篇文章,原本打算取名<Angular单页面应用基于Ocelot API网关与IdentityServer4+ASP.NET Iden ...
- 解决linux下启动tomcat找不到jdk
在tomcat目录下 vim catalina.sh 头部加入 JAVA_HOME='/root/use/local/java/jdk/';export JAVA_HOME;
- JavaScript中foreach、map、filter、find、every、some的用法
foreach:只是循环数组中的每一项,没有返回值 如: var arr = [2,3,3,4,5,6]; arr.foreach(function(item,index,array){ dosom ...
- AD17无法复制原理图到Word的解决方法
标题: 解决AD17无法复制原理图到WORD 作者: 梦幻之心星 347369787@QQ.com 标签: [AD, Word, 原理图] 目录: 软件 日期: 2019-3-17 目录 前提说明: ...
- python调用大漠插件教程01注册大漠
使用大漠有两种方法,一种是直接调用特殊的dll实现不注册就能使(本人不会),另一种则是注册后使用. 如何用python注册大漠? from win32com.client import Dispatc ...
- RabbitMQ安装(centos7)
本文是作者原创,版权归作者所有.若要转载,请注明出处. 本文RabbitMQ版本为rabbitmq-server-3.7.17,erlang为erlang-22.0.7.请各位去官网查看版本匹配和下载 ...
- java方法句柄-----5.Method Handles in Java
Method Handles in Java 目录 Method Handles in Java 1.介绍 2.什么是MethodHandle 3. Method Handles vs Reflect ...
- Java实现 蓝桥杯VIP 算法训练 无权最长链
试题 算法训练 无权最长链 问题描述 给定一个n节点m边的无圈且连通的图,求直径 输入格式 第一行两个数字n,m 接下来m行每行两个数字x,y,代表x,y之间有一条边 输出格式 要求用户的输出满足的格 ...
- Java实现 LeetCode 38 外观数列
38. 外观数列 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述.前五项如下: 1 11 21 1211 111221 1 被读作 "one 1" ...