A. The Monster
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times b, b + a, b + 2a, b + 3a, ...and Morty screams at times d, d + c, d + 2c, d + 3c, ....

The Monster will catch them if at any point they scream at the same time, so it wants to know when it will catch them (the first time they scream at the same time) or that they will never scream at the same time.

Input

The first line of input contains two integers a and b (1 ≤ a, b ≤ 100).

The second line contains two integers c and d (1 ≤ c, d ≤ 100).

Output

Print the first time Rick and Morty will scream at the same time, or  - 1 if they will never scream at the same time.

Examples
input
20 2
9 19
output
82
input
2 1
16 12
output
-1
和蓝桥杯凑包子数目是一个类型的题目
先介绍一下扩展欧几里德定理
对于不完全为 0 的整数 a,b,gcd(a,b)表示 a,b 的最大公约数。那么一定存在整
数 x,y 使得 gcd(a,b)=ax+by。
对于这道题目 我们可以整理出 b+x*a=d+y*c    那么就有 ax+(-yc)=d-b
那么可不可达 我们就看d-b能否为gcd(a,c)的倍数了
上代码
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
#include <queue>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;
int gcd(int x,int y)
{
if(y==0) return x;
else return gcd(y,x%y);
}
// 是否可达的理解
int main()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
int pos=(d-b);
if(pos%gcd(a,c)!=0) cout<<-1<<endl;
else
{
while(b!=d)
{
if(b < d) b+=a;
else d+=c;
}
cout<<b<<endl;
}
return 0;
}

  

 

Codeforces Round #406 (Div. 2) A MONSTER的更多相关文章

  1. 【扩展欧几里得】Codeforces Round #406 (Div. 2) A. The Monster

    扩欧,a+bx=c+dx,输出x>=0且y>=0,且a+bx最小的解. 要注意不能只保证x非负,还得看看能否保证y也非负. #include<cstdio> #include& ...

  2. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

  3. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  4. 维护前面的position+主席树 Codeforces Round #406 (Div. 2) E

    http://codeforces.com/contest/787/problem/E 题目大意:给你n块,每个块都有一个颜色,定义一个k,表示在区间[l,r]中最多有k中不同的颜色.另k=1,2,3 ...

  5. 区间->点,点->区间,线段树优化建图+dijstra Codeforces Round #406 (Div. 2) D

    http://codeforces.com/contest/787/problem/D 题目大意:有n个点,三种有向边,这三种有向边一共加在一起有m个,然后起点是s,问,从s到所有点的最短路是多少? ...

  6. 有向图博弈+出度的结合 Codeforces Round #406 (Div. 2) C

    http://codeforces.com/contest/787/problem/C 题目大意:有一个长度为n的环,第1个位置是黑洞,其他都是星球.已知在星球上(不含第一个黑洞)有一位神.有两个人, ...

  7. 【Codeforces Round #406 (Div. 2)】题解

    The Monster 签到题,算一下b+=a和d+=c,然后卡一下次数就可以了. Not Afraid 只要一组出现一对相反数就是安全的. Berzerk 题意:[1,n],两个人轮流走,谁能走到1 ...

  8. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  9. Codeforces Round #406 (Div. 1)

    B题打错调了半天,C题想出来来不及打,还好没有挂题 AC:AB Rank:96 Rating:2125+66->2191 A.Berzerk 题目大意:有一个东东在长度为n的环上(环上点编号0~ ...

随机推荐

  1. 取模的n种情况

    一  减法 (a-b)%mod=(a%mod-b%mod+n)%mod; 二 大数 有乘法取模 可推出 如下代码 string s; cin>>s; ,len=s.length(); ;i ...

  2. LeetCode 128. 最长连续序列(Longest Consecutive Sequence)

    题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...

  3. 黑马vue---16、vue中通过属性绑定为元素设置class类样式

    黑马vue---16.vue中通过属性绑定为元素设置class类样式 一.总结 一句话总结: 这里就是为元素绑定class样式,和后面的style样式区别一下 vue中class样式绑定方式的相对于原 ...

  4. No suitable constructor was found in NUnit Parameterised tests

    No suitable constructor was found in NUnit Parameterised tests Fairly obvious, but can also happen i ...

  5. Qt之CMake和MinGW编译OpenCV

    这里编译的是最新版OpenCV3.4.3,编译过程比较曲折,大部分编译错误都可以查得到解决方法,也遇到了Google无果的错误,好在最后还是解决了,特此记录. 编译环境 win10-64bit Qt5 ...

  6. AIX系统命令

    当/oracle达到一定使用量时需清除*.trc文件find . -name "*.trc"|xargs rm -rf 1.查看是否开启归档日志切换用户:su - oracle使用 ...

  7. Selenium 2自动化测试实战33(带unittest的脚本分析)

    带unittest的脚本分析 #test.py #coding:utf-8 from selenium import webdriver from selenium.webdriver.common. ...

  8. GWO(灰狼优化)算法MATLAB源码逐行中文注解(转载)

    以优化SVM算法的参数c和g为例,对GWO算法MATLAB源码进行了逐行中文注解. tic % 计时器 %% 清空环境变量 close all clear clc format compact %% ...

  9. 一次神奇的JVM调优

    老张接个新项目,项目可是不小,好多模块.使用Intellij import new project, 结果卡在writing class中,而且mac的风扇一直转,像是要变成直升机起飞. 等啊等,in ...

  10. playbook文件内容解释

    [root@node1 playbook]# cat nginx.yml - hosts: test \\主机组,要和nginx.yml在同一个目录下 remote_user: root \\远端执行 ...