codeforces787A-The Monster

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.

Example

Input
20 2
9 19

Output
82

Input
2 1
16 12

Output
-1

Note

In the first sample testcase, Rick's 5th scream and Morty's 8th time are at time 82.

In the second sample testcase, all Rick's screams will be at odd times and Morty's will be at even times, so they will never scream at the same time.

【分析】已知a,b,c,d,求使得a+b*i == c+ d*j 成立的最小a+b*i 。

暴力枚举

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10000;
int n[maxn],m[maxn];
int main()
{int a,b,c,d;while(~scanf("%d%d%d%d",&a,&b,&c,&d)){int flag=0;for(int i=0;i<maxn;i++){n[i] = b + a*i;m[i] = d + c*i;}int i,j;for(i=0;i<maxn;i++){for(j=0;j<maxn;j++){if(n[i]==m[j]){flag=1;break;}}if(flag)break;}if(flag){printf("%d\n",n[i]);}elseprintf("-1\n");}return 0;
}

codeforces787A-The Monster相关推荐

  1. DumpMem and Monster - Virtual Memory Explorers on Windows Mobile/CE

    Windows Mobile 5 和 6的平台是建立在CE5.x的基础上的.当可用的内存很少时,平台会自动关闭应用程序.而且,在这个移动平台上,同时只能运行32个process.在桌面电脑上,我们有4 ...

  2. 2021牛客多校6 - Gambling Monster(分治FWT优化期望dp)

    题目链接:点击查看 题目大意:有一个转盘,每次转动得到 0∼n−10\sim n−10∼n−1(nnn 是 2 的幂)的概率分别给出.最开始你有一个数 x=0x=0x=0,每次转动转盘得到一个数 yy ...

  3. 【BZOJ】【3856】Monster

    又是一道水题-- 重点是分情况讨论: 首先我们很容易想到,如果a*k-b*(k+1)>0的话那么一定能磨死Monster. 但即使不满足这个条件,还有可能打死boss: 1.h-a<1也就 ...

  4. 2021牛客暑期多校训练营6 :D Gambling Monster 期望dp + fwt + cdq分治

    传送门 文章目录 题意: 思路: 题意: 给你一个大轮盘,被分成了nnn个区域0,1,2,..,n−10,1,2,..,n-10,1,2,..,n−1,每个区域被转到的概率是ai∑j=0n−1aj\f ...

  5. Monster Hunter(2020南京M)

    Monster Hunter(2020南京M) 题意: 给你一颗树,树上每个节点都是一个hpi 血量的怪物.打败每个怪物所需要的能量值为hpi + 所 有 存 活 的 直 接 子 节 点 的 hpj ...

  6. [2.7]【CF933A】A Twisty Movement【CF926B】Add Points【CF917A】The Monster【CF919E】Congruence Equation

    文章目录 T1:A Twisty Movement 题目 题解 code T2:Add Points 题目 题解 code T3:The Monster 题目 题解 code T4:Congruenc ...

  7. M. Monster Hunter(树形dp)

    M. Monster Hunter 才知道原来树形dp是三维的,一直没有学会过,感谢大佬的文章!算法进阶-理解树形背包问题 状态表示:fi,k,j,{0/1}f_{i,k,j,\{0/1\}}fi,k ...

  8. 线段树-Pudding Monster CF526F-单调栈

    Pudding Monster 题目连接:https://www.luogu.org/problem/show?pid=CF526F 问题提出 给长度为nnn的排列AAA. 问有多少(l,r)(l,r ...

  9. B. The Monster and the Squirrel

    B. The Monster and the Squirrel Ari the monster always wakes up very early with the first ray of the ...

  10. 计算机音乐monster,【MJJ】【MJ音乐汇总系列】Monster 怪物

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 Monster (Featuring 50 Cent) 怪物[歌词 英文+中文] You can look at them coming out the ...

最新文章

  1. 01:谁考了第k名 个人博客:doubleq.win
  2. i++ 和++i的区别
  3. 服务器异常代码413问题
  4. 第二阶段个人总结06
  5. linux 更改文件所有者
  6. jQuery-点击按钮实现回到顶部的两种方式
  7. CMake基础 第4节 动态库
  8. HDU - 4388 Stone Game II(博弈+思维)
  9. 人类一败涂地显示服务器,人类一败涂地怎么开服务器 | 手游网游页游攻略大全...
  10. 盘点那些面试中最常问的MySQL问题
  11. Unix网络编程卷一第三章笔记
  12. 用python实现websocket请求遇到的问题及解决方法。
  13. 5 Expressing future time
  14. DP:最大公共子序列
  15. 海思Hi3798MV300_Hi3798MV300H_Datasheet-系统
  16. 关于春天的古诗(关于春天的古诗有哪些)
  17. vos网络电话系统怎样搭建?
  18. Python语言程序设计基础_期末作品设计——收银软件(2020-2 B)_答案_通识教育必修课程_上海师范大学
  19. JAVA微信会员卡接口踩坑
  20. Can‘t connect to MySQL server on ‘localhost‘ (10061)

热门文章

  1. 《聆听宇宙的歌唱》——超越故乡
  2. SATA接口的硬件基础知识
  3. LGE-MRI是什么呢?
  4. Java字符串拆分详解
  5. 如何从Win10升级到Windows11正式版
  6. 闲置资源组建NAS存储服务器
  7. 基于OpenCV的简易实时手势识别(含代码)
  8. MVVM+Retrofit+Kotlin网络框架封装
  9. 【Java学习笔记】1:Java语言基础
  10. 乌班图16设置固定(静态)ip