【扩展欧几里得】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 ...
随机推荐
- px,em,rem字体单位
1.px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的.(引自CSS2.0手册) 2.em是相对长度单位.相对于当前对象内文本的字体尺寸,em存在值继承问题. 浏览器的默认字 ...
- C++获取系统时间的方法
//首先是了解这个结构体,_SYSTEMTIME ,然后通过系统函数GetLocalTime往这个结构体的变量中写入当前系统时间typedef struct _SYSTEMTIME { WORD wY ...
- Kaggle机器学习之模型集成(stacking)
Stacking是用新的模型(次学习器)去学习怎么组合那些基学习器,它的思想源自于Stacked Generalization这篇论文.如果把Bagging看作是多个基分类器的线性组合,那么Stack ...
- 【自己练习】linux常见命令——(六)
菜鸟教程命令大全 http://www.runoob.com/linux/linux-command-manual.html 命令大全: http://man.linuxde.net/ ta ...
- nmap导出处理脚本
import sys log = open("result.gnmap","r") xls = open("output.csv",&quo ...
- MySQL:Can't create test file XXX.lowe-test
问题说明 今天部署MySQL,在使用mysql_install_db,初始化数据库时报如下错误 180622 11:36:38 mysqld_safe Starting mysqld daemon w ...
- vmware安装ubuntu " Intel VT-x 处于禁用状态"
vmware安装ubuntu " Intel VT-x 处于禁用状态" http://jingyan.baidu.com/article/fc07f98976710e12ffe51 ...
- caffe solver.prototxt 生成
from caffe.proto import caffe_pb2 s = caffe_pb2.SolverParameter() path='/home/xxx/data/' solver_file ...
- yum安装的Apache的各种配置文件的位置
//配置文件 /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.d/README /etc/httpd/conf.d/proxy_ajp.conf / ...
- 关于那些Android中不常用的设置属性
很多在manifest中的属性我们经常遗忘了它们,或者经常看到但又不是很明白它的作用.那么在这里我就拿了一些属性简单的解释一下,防止以后碰到却不知道其中的意思.不是很全,以后会断断续续的补充吧 一.a ...