zoukankan      html  css  js  c++  java
  • 787A The Monster

    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
    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.

     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 #define LL long long
     5 #define INF 0x3f3f3f3f
     6 #define N 10000
     7 int arr[N], brr[N];
     8 
     9 int main()
    10 {
    11     int a, b, c ,d;
    12     scanf("%d%d%d%d",&a,&b,&c,&d);
    13     for(int i = 0;i < N; i++) {
    14         arr[i] = b+ a*i;
    15         brr[i] = d+ c*i;
    16     }
    17     int k;
    18     int flag = 0;
    19     for(int i = 0;i < N; i++) {
    20         for(int j = 0;j < N; j++) {
    21             if(arr[i] == brr[j]) {
    22                 flag = 1;
    23                 k = i;
    24                 break;
    25             }
    26         }
    27         if(flag) break;
    28     }
    29     if(flag) printf("%d
    ",arr[k]);
    30     else puts("-1");
    31 return 0;
    32 }
  • 相关阅读:
    Tomcat线程参数
    CDH平台规划注意事项
    python 不同数据类型的序列化
    Python 中__new__方法详解及使用
    线程生命周期
    如何在JAVA中每隔一段时间执行一段程序
    手动开启是事务提交回滚
    MySQL数据类型转换函数CAST与CONVERT的用法
    mybatis插入是返回主键id
    解决dubbo注册zookepper服务IP乱入问题的三种方式
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6631793.html
Copyright © 2011-2022 走看看