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 ...
随机推荐
- IE8/IE9/IE10打开网页后,自动设置浏览器模式为“IE8/IE9/IE10兼容性视图”
http://blog.snsgou.com/post-16.html 作者:SNSGOU 发布于:2013-07-17 22:23:05 分类:CSS/HTML 评论(0) 浏览(132 ...
- C++98/11/17表达式类别
目标 以下代码能否编译通过,能否按照期望运行?(点击展开) #include <utility> #include <type_traits> namespace cpp98 ...
- 关于docker的常见使用命令
1. Docker的启动与停止 systemctl命令是系统服务管理器指令 启动docker: systemctl start docker 停止docker: systemctl stop dock ...
- iOS开发图片选择
一行代码搞定图片选择 // // gzhPhotoManager.h // 图片选择 // // Created by 郭志贺 on 2020/5/26. // Copyright © 2020 郭志 ...
- MIT6.828准备:MacOS下搭建xv6和risc-v环境
本文介绍在MacOS下搭建Mit6.828/6.S081 fall2019实验环境的详细过程,包括riscv工具链.qemu和xv6,对于Linux系统同样可以参考. 介绍 只有了解底层原理才能写好上 ...
- 微信小程序开发 -- 通过云函数下载任意文件
微信小程序开发 -- 通过云函数下载任意文件 1.云开发介绍 微信小程序开发者众所周知,小程序开发拥有许多限制,当我还是一个菜鸟入门的时候,第一关就卡在了没有备案域名的HTTP请求上面,那时候云开 ...
- 01.Django-基础
基础 1. 简介 Django是一个由Python写成的开放源代码的重量级Web应用框架. Django的目的是使常见的Web开发任务,快速和容易 2. 特点 MVC开发模式 内置进行快速web开发所 ...
- python之robotframework+pycharm测试框架
一.robotframework简介 Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式 ...
- Java中方法的重载与重写
1.方法的名字和参数列表称为方法的签名:每个方法具有唯一与其对应的签名: 2.方法的重载:在某个类中,存在具有多个相同名字不同参数列表的方法,称之为重载: 被重载的方法必须改变参数列表(参数个数或类型 ...
- Rocket - tilelink - SRAM
https://mp.weixin.qq.com/s/-z9n6SHyAiK2OE7mOSvC2Q 简单介绍SRAM的实现. 1. 基本介绍 实现一个支持读写的静态存储器.存取的 ...