Codeforces Round #406 (Div. 2) A MONSTER
1 second
256 megabytes
standard input
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.
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).
Print the first time Rick and Morty will scream at the same time, or - 1 if they will never scream at the same time.
20 2
9 19
82
2 1
16 12
-1
和蓝桥杯凑包子数目是一个类型的题目
先介绍一下扩展欧几里德定理
#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的更多相关文章
- 【扩展欧几里得】Codeforces Round #406 (Div. 2) A. The Monster
扩欧,a+bx=c+dx,输出x>=0且y>=0,且a+bx最小的解. 要注意不能只保证x非负,还得看看能否保证y也非负. #include<cstdio> #include& ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- 维护前面的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. 2)】题解
The Monster 签到题,算一下b+=a和d+=c,然后卡一下次数就可以了. Not Afraid 只要一组出现一对相反数就是安全的. Berzerk 题意:[1,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. 1)
B题打错调了半天,C题想出来来不及打,还好没有挂题 AC:AB Rank:96 Rating:2125+66->2191 A.Berzerk 题目大意:有一个东东在长度为n的环上(环上点编号0~ ...
随机推荐
- js 继承的一个例子
<script type="text/javascript"> function Animal(){ this.species = "动物"; th ...
- ubuntu 安装eclipse for c++
linux的GUI和windos比起来实在逊色,虽然它的终端模式(命令行模式)非常强大.linux发行版ubuntu的GUI相对其他版本要华丽一些,所以最近由redhat转向ubuntu进行linux ...
- 取模的n种情况
一 减法 (a-b)%mod=(a%mod-b%mod+n)%mod; 二 大数 有乘法取模 可推出 如下代码 string s; cin>>s; ,len=s.length(); ;i ...
- koa 项目实战(六)注册接口加密
1.创建工具类 根目录/config/tools.js const bcrypt = require('bcryptjs'); const tools = { enbcrypt(password) { ...
- SpringCloud(一)之微服务核心组件Eureka(注册中心)的介绍和使用
一 Eureka服务治理体系1.1 服务治理服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现. Spring Cloud Eureka是Spring Clou ...
- linux服务之redis
redis wget http://download.redis.io/releases/redis-4.0.6.tar.gzcd redis-4.0.6makecd src/./redis-serv ...
- 短信的内容提供者Uri和短信表结构
* sms表 * address :手机号码 * date :收发短信的时间 * read :短信的阅读状态 1,已读 0,未读 * type :收发短信的类型 1,收到短信 2,发出短信 * bod ...
- 建立Maven工程时出错,Failure to transfer
建立Maven工程时出错,Failure to transfer com.thoughtworks.xstream:xstream:jar:1.3.1 Failure to transfer com. ...
- redis集群 应该注意的问题
redis cluster注意的问题 : ‘cluster-require-full-coverage’参数的设置.该参数是redis配置文件中cluster模式的一个参数,从字面上基本就能看出它的作 ...
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...