[模板][洛谷P1516]青蛙的约会(exgcd)

洛谷 P5656 【模板】二元一次不定方程(exgcd) 给一个二元一次不定方程 ax+by=c ax+by=c ax+by=c 无整数解:输出 -1 有整数解: 有正整数解:输出解的个数,输出 x,yx,yx,y 的最小值和最大值 无正整数解:输出 x,yx,yx,y 的最小正整数值 思路:扩展欧几里得 void exgcd(int a,int b,int &x,int &y){ if(!b){x=1,y=0;return;} exgcd(b,a%b,x,y); int p=x; ... 阅读详情

题目←

设k步后相遇
题目实际要求

X+m∗k≡Y+n∗k(modL)

不写成同余方程的话,就是
X+m∗k−q∗L=Y+n∗k−p∗L

移项
(m−n)∗k+L∗(p−q)=Y−X

m - n –>a,L –>b,Y - X –>c,k –> x,p - q –>y
那么就是熟悉的ax + by = c了
众所周知exgcd解得是ax + by = gcd(a,b)这个方程
所以将c写成gcd(a,b)*t//若不能整除gcd(a,b),无解
可以先解出ax+by = gcd(a,b)的解和通解,再乘上t即为原方程解

关于通解
在ax+by = gcd(a,b)中,已知一组解x,y
x0 = x + (b/gcd(a,b))*t,y0 = y - (a/gcd(a,b))*t
正负号可颠倒

关于最小正整数解
设m = b/gcd(a,b),
最小正整数解 X = (x%m + m)%m
//处理取模时出现的负数可用

//负数也可以正常扩欧 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define LL long long
using namespace std;
LL X,Y,N,M,L,x,y,a,b,m,G;
LL Gcd(LL a,LL b){
    return b ? Gcd(b,a%b) : a;
}
void exgcd(LL a,LL b){
    if(!b){
        x = 1;
        y = 0;
        return;
    }
    exgcd(b,a%b);
    LL tmp = y;
    y = x - (a/b)*y;
    x = tmp;
}
int main(){
    cin >> X >> Y >> M >> N >> L;
    G = Gcd(M - N,L);
    if((Y - X)%G){
        printf("Impossible");
        return 0;
    }
    LL t = (Y - X)/G;//
    m = L/G;//X0 = X +- (b/gcd); 
    a = (M - N)/G;b = L/G;//!!!(M - N)/t,L/t->t
    exgcd(a,b);
    x *= t;//一组解 
    m = abs(m);//若m为负,相当于 -= m 
    x = (x%m + m)%m;
    cout << x;
}
Exgcd(拓展欧几里得算法)的初步理解 若a,b是整数,且gcd(a,b)=d,那么对于任意的整数x,y,ax+by都一定是d的倍数,特别地,一定存在整数x,y,使ax+by=d成立。它的一个重要推论是:a,b互质的充分必要条件是存在整数x,y使ax+by=1.针对于一次不定方程ax+by=c进行求解,利用以上的裴蜀定理可以进行求解,当然要满足 gcd(a,b)|c 这个前置情况这个时候实际上就是对于b*x+(a%b)*y=d(辗转相除法求得)a%b=a-(a/b)*b进行带入即可得到:a*y-b*(x-(a/b)*y)=d和原式ax+by=d对 阅读详情

相关推荐

Zabbix安装与部署 系统环境:centos7.0

Zabbix安装与部署 系统环境:centos7.0

weixin_42485027的博客 2600

P1516 青蛙的约会 exgcd

Link 思路: 初始位置x, y, 每次跳跃距离x, y, 圈长l,即解方程(n − m) * t ≡ x − y (mod l),即(n −m)t+kl=x−y,设a = n - m, b = l, c = x - y, 即求at + bk= c 的t最小整数解。 设g = gcd(a, b), 可求出at + bk= g的t的特解t0 由x = (x % (b / d) + b / d) % (b / d);找到最小整数解,即为答案 注意如果a小于0,即n-m<0,则要把...

Strezia的博客 317

Vivado图形化界面烧写MCS文件到SPI FLASH全流程(附常见报错解决方案)

本文详细介绍了使用Vivado图形化界面将MCS文件烧录至SPI FLASH的完整操作流程。内容涵盖从环境准备、MCS文件生成到硬件连接与烧写的每一步,并针对SPI总线宽度报错、FLASH型号识别失败等常见问题提供了深度解决方案,帮助用户高效完成FPGA配置,摆脱对TCL命令行的依赖。

s8t9u0v1w的博客 536

洛谷 P1082 同余方程(同余&&exgcd)

嗯... 题目链接:https://www.luogu.org/problem/P1082 这道题很明显涉及到了同余和exgcd的问题,下面推导一下: 首先证明有解情况:     ax + by = m有解的必要条件是 m mod gcd(a, b) = 0     a为gcd(a, b)的倍数,b为gcd(a, b)的倍数,x、y为整数,     所以ax + ...

weixin_30871905的博客 256

FPGA实战:手把手教你实现VESA DSC编码(附Verilog代码解析)

本文详细介绍了如何使用FPGA实现VESA DSC编码器,包括核心架构设计、Verilog代码实现与优化技巧。通过流水线设计、色彩空间转换和预测编码等关键技术,帮助开发者掌握工业级DSC编码器的开发流程,并提供实用的调试与性能优化方法。

weixin_29165725的博客 404

ubuntu为Docker配置代理

我们平常在ubuntu终端中使用curl或git命令时,往往会很慢。所以,首先需要给ubuntu终端环境添加代理。查看自身那个软件的端口号,我这里是7890。

不想随波逐流的大学生一枚,我们每个人都在努力奔跑 1643

[POJ1601]青蛙的约会(exgcd)

今晚真是石乐志

薇小薇 433

postgresql 数据库 timescaledb 函数time_bucket_gapfill()报错解决及更换 license

最近在工作中使用postgresql 和 timescaledb 的time_bucket_gapfill()函数 报错,让更换license,在网上找了一大圈,没有找到,最后解决了这个问题,这篇文章记录我的解决办法查看timescaledb.license参数命令:show timescaledb.license;修改命令: 结果不行,报错如下:lter system set timescaledb.license=‘timescale’;那只能换包了如果出现上面那种情况,那我们只能先从官网上下载带 '

yang_z_1的博客 3249

EXGCD:青蛙的约会

题目好暴力啊 题目链接:青蛙的约会 代码:(里面有注释) //EXGCD:返回的值为最大公约数,但是里面已经处理了x,y #include&lt;cstdio&gt; #include&lt;iostream&gt; #include&lt;algorithm&gt; #define ll long long using namespace std; ll x,y,n,m,l,a,b; ...

yun_xing_的博客 261

Oracle EBS Interface/API(43) -自定义AP发票状态说明API

在某项目,客户需求获得AP发票的状态说明,例如对账系统或者某些报表。由于标准系统未提供状态说明,需要通过AP发票的标准Form获得相关业务逻辑,然后自定义AP发票状态说明API。

陈显平的专栏-Oracle EBS 606

ICC2:使用analyze_lib_cell_placement检查lib cell的pass rate

我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧?在place之前需要使analyze_lib_cell_placement命令去检查lib cell在当前的site row/power plan/legalize设置的环境下能够正常place和legalize。下面分享一个脚本去报告pass rate低于2%的lib cell。

m0_61544122的博客 675

P1516 青蛙的约会 洛谷

https://www.luogu.org/problem/show?pid=1516 题目描述 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去...

agcozdwdfvds08078的博客 107

消息平台接入实战:Hermes Agent 实现微信/钉钉日常任务自动化的 4 步配置

揭示 Hermes Agent 接入微信/钉钉时高频失效的根本原因——非 API 调用问题,而是上下文边界缺失;详解通过 soul.md 锁定人格基线、skills/ 目录原子化封装技能、CLAUDE.md 控制记忆节奏、Docker 环境适配四步构建可信赖自动化信任链,并给出身份证号过滤、中文标点

weixin_42520239的博客 165

p1516&poj1061&bzoj1477 青蛙的约会

传送门(洛谷) 题目 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。...

weixin_34088583的博客 138

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【初等数论】裴蜀定理与扩展欧几里得算法

可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。奇怪的是,虽然野人有很多,但没有任何两个野人在有生之年处在同一个山洞中,使得小岛一直保持和平与宁静,这让科学家们很是惊奇。ml 酒杯中剩下的酒的体积尽可能小,他请求你帮助他设计倾倒的方案。Winy 是一家酒吧的老板,他的酒吧提供两种体积的啤酒,

guileless的博客 684

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

铁板铮铮♂+习题集

数论 快速幂 当b为偶数:ab=ab/2* ab/2 当b为奇数:ab=ab/2* ab/2* a 核心代码: ll quickpow(ll a,ll b) { ll ret=1; while(b) { if(b%2==1) ret=ret*a%P; a...

weixin_30498921的博客 137

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

(exgcd)青蛙的约会

拓展gcd详解传送门 思路:设时间为t,则两个青蛙的位置分别为(x+mt)mod L、(y+nt) mod L,相遇即是(x+mt)%L=(y+nt)%L,即(m-n)t+kL=y-x。 OK,现在已经符合ax+by=c的方程了,设a=m-n,b=L,c=y-x,然后套用模板求出特解t的值,注意t>0,所以要用通解公式得出最小正整数 解释<1> gcd 只对非负整数有意义,所以如...

LIRONGAO0706的博客 279

POJ 青蛙的约会 exgcd

不想搞数论啊啊啊QAQ贴学长博客题解 : 【poj1061】青蛙的约会 exgcd解同余方程我的代码:#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <cmath> using namespace std; typedef long long LL;LL exgcd(LL a, LL

QER 的博客 2323

洛谷P1516 青蛙的约会 题解

两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。米,两只青蛙跳一次所花费的时间相同。

qq_56071472的博客 446

洛谷 P1516 青蛙的约会

题目:青蛙的约会   思路: 扩展欧几里得算法求出特解,再由特解求出通解及最小值。 依题意,可列出方程(n-m)x1+Lx2=X-Y。 根据exgcd(n-m,L,x1,y1),求出特解(x1,y1),以及gcd(n-m,L)。 注意一下,当(X-Y)%gcd!=0时原方程无解。 再根据公式,最小值为(x0%t+t)%t,其中x0=x1*c/gcd,t=b/gcd。 通解公式:x=...

iwi 316

洛谷P1516 青蛙的约会

线性同余方程

forezxl的博客 611

洛谷P1516 青蛙的约会(扩展欧几里得EXGCD)

洛谷P1516 青蛙的约会 TITLE 思路 ax+by=c存在整数解当且仅当gcd(a,b)|c 扩展欧几里得算法实现求解二元一次不定方程的整数解 若求出ax’+by’=gcd(a,b),那么将等式两边变为原来的c/gcd(a,b)倍 long long exgcd(long long a,long long b,long long &x,long long &y) { if(!b){x=1,y=0;return a;} long long ans=exgcd(b,a%b,x,y),t

weixin_46975572的博客 210

【题解】洛谷P1516 青蛙的约会

题目 洛谷P1516青蛙的约会 题解 假设跳了t次以后相遇。列式变形后,解同余方程。 注意:欧几里得算法通常情况下默认参数a,b为正数,同时gcd(a,b)也为正数,所以要将参数转成正数,此处提供两种常用方法。 方法一:同余方程是在模意义下的,取参数在模意义下的最小正数即可。 #include <bits/stdc++.h> using namespace std; typedef long long ll; ll p1,p2,len,s1,s2,x,

zjgmartin的博客 469

洛谷 P1516

拓展欧几里得

Orion_Rigel 431

洛谷 P1516 青蛙的约会 exgcd

题目描述两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的

liangzihao1的博客 283
上一篇: [模板][洛谷P1835]素数密度(区间筛)
下一篇: [codevs1213]解的个数 二分 + exgcd
LOI_pingxing
博客等级 码龄10年 24粉丝 103原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值