【扩展欧几里得】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 ...
随机推荐
- div+css实现表头固定内容滚动表格
<div class="m-demo"> <table> <thead> <tr><th>定宽a</th>& ...
- Python eval 函数说明
eval(str [,globals [,locals ]]) -- 函数将字符串str当成有效Python表达式来求值,并返回计算结果. 例 : eval('3+4') ==> ...
- python碎片记录(一)
1.python中求幂运算 2**31-1 2的31次方减一(32位整型数据范围为-2^31~2^31-1,最高位为符号位,负向值比正向值绝对值大1) 2.python中整数除法 x=x//1 ...
- KVM虚拟机建立快照
部分转载: http://blog.csdn.net/gg296231363/article/details/6899533 windows虚拟机默认镜像格式为raw,快照默认格式为qcow2.win ...
- SD 模拟sip 读写子程序
void simulate_spi_write_byte(u8 data){ u8 kk; SPI3_CS(0); SPI3_SCK(0); delay_us(1); //???spi???1/2us ...
- $scope作用及模块化解决全局问题
$scope对象就是一个普通的JavaScript对象,我们可以在其上随意修改或添加属性.$scope对象在AngularJS中充当数据模型,但与传统的数据模型不一样,$scope并不负责处理和操作数 ...
- WA时查错点
这篇文章旨在总结可能出错的原因,想到时随时会补充. 查看调试输出语句是否删除 查看数组是否清零 查看是否使用long long 查看是否有的常量应开LL(如1LL << (32) ) 查看 ...
- android intent 传数据
1. 基本数据类型 Intent intent = new Intent(); intent.setClass(activity1.this, activity2.class); //描述起点和目标 ...
- LeetCode212. Word Search II
https://leetcode.com/problems/word-search-ii/description/ Given a 2D board and a list of words from ...
- PHP给图片加水印具体实现
给图片加水印实现方法如下: class Mark { public function __construct() { } /** * 加水印 * @param file $srcImg 要加水印的图片 ...