【扩展欧几里得】Codeforces Round #406 (Div. 2) A. The Monster
扩欧,a+bx=c+dx,输出x>=0且y>=0,且a+bx最小的解。
要注意不能只保证x非负,还得看看能否保证y也非负。
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
ll a,b,c,d;
void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b)
{
d=a;
x=1;
y=0;
}
else
{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
int main(){
// freopen("a.in","r",stdin);
cin>>b>>a>>d>>c;
ll x0,y0,D;
exgcd(b,d,D,x0,y0);
if((c-a)%D){
puts("-1");
return 0;
}
x0=x0*((c-a)/D);
x0=(x0%(d/D)+d/D)%(d/D);
y0=(a+b*x0-c)/d;
if(y0>=0){
cout<<a+x0*b<<endl;
return 0;
}
exgcd(d,b,D,x0,y0);
if((a-c)%D){
puts("-1");
return 0;
}
x0=x0*((a-c)/D);
x0=(x0%(b/D)+b/D)%(b/D);
y0=(d*x0+c-a)/b;
if(y0<0){
puts("-1");
return 0;
}
cout<<c+x0*d<<endl;
return 0;
}
【扩展欧几里得】Codeforces Round #406 (Div. 2) A. The Monster的更多相关文章
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- 维护前面的position+主席树 Codeforces Round #406 (Div. 2) E
http://codeforces.com/contest/787/problem/E 题目大意:给你n块,每个块都有一个颜色,定义一个k,表示在区间[l,r]中最多有k中不同的颜色.另k=1,2,3 ...
- 区间->点,点->区间,线段树优化建图+dijstra Codeforces Round #406 (Div. 2) D
http://codeforces.com/contest/787/problem/D 题目大意:有n个点,三种有向边,这三种有向边一共加在一起有m个,然后起点是s,问,从s到所有点的最短路是多少? ...
- 有向图博弈+出度的结合 Codeforces Round #406 (Div. 2) C
http://codeforces.com/contest/787/problem/C 题目大意:有一个长度为n的环,第1个位置是黑洞,其他都是星球.已知在星球上(不含第一个黑洞)有一位神.有两个人, ...
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #406 (Div. 2) A MONSTER
A. The Monster time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #406 (Div. 1)
B题打错调了半天,C题想出来来不及打,还好没有挂题 AC:AB Rank:96 Rating:2125+66->2191 A.Berzerk 题目大意:有一个东东在长度为n的环上(环上点编号0~ ...
- Codeforces #Round 406(Div.2)
来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------- 大家好,我是一个假人.在学习O ...
随机推荐
- jQuery清空表单方法
$(':input', '#form1') .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked') .rem ...
- base--AuditResult
//参考base-4.0.2.jar public class AuditResult implements TimeReferable, Serializable //参考api-1.0.0.jar ...
- javascript中null与undefined的区别
1.null null是一个对象,表示一个空对象指针,typeof(null)返回object,null参与运算时会转化为0,将对象初始化为null,可以知道变量是否保存了对象的引用 2.undefi ...
- 分类算法:决策树(C4.5)(转)
C4.5是机器学习算法中的另一个分类决策树算法,它是基于ID3算法进行改进后的一种重要算法,相比于ID3算法,改进有如下几个要点: 1)用信息增益率来选择属性.ID3选择属性用的是子树的信息增益,这里 ...
- Perl6 Bailador框架(3):路径匹配
use v6; use Bailador; =begin pod 注意的是, 当/:one设置时 虽然你有/admin或/about, 但这个/:one不会跟现有的匹配 只跟没有的匹配: 也就是说, ...
- 74cms 注入exp
遇到就瞎写了一个: #!/usr/bin/env python #encoding:utf-8 #by i3ekr import requests,optparse,re parse = optpar ...
- docker使用小记
查看当前镜像:docker images 运行一个简单的镜像:docker run hello-world 拉取一个远程docker:docker pull centos docker中安装nginx ...
- Python脚本 - 查询磁盘的读写次数信息
测试系统为:Centos 6.7 Python版本为: 3.6.4 脚本功能:查看指定磁盘的读写及时间等相关信息 #!/usr/bin/env python3 from collections imp ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 檢查 cpu 的全部 gpio 狀態及設定
$ adb root # cat /sys/kernel/debug/gpio