787A - The Monster - CodeForces Solution


brute force math number theory *1200

Please click on ads to support us..

Python Code:

(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))

first_b = b
first_d = d
pick_d = 0
pick_b = 0
not_pick_b = 0
not_pick_d = 0

if b == d:
    print(b)
else:
    while True:
        if (b - first_d) % c == 0 and b - first_d != 0 and (b - first_d) // c > 0:
            pick_b += 1
            break
        else:
            if b > 10000:
                not_pick_b += 1
                break
        b += a


    while True:
        if (d - first_b) % a == 0 and (d - first_b)!= 0 and (d - first_b) // a > 0:
            pick_d += 1
            break
        else:
            if d > 10000:
                not_pick_d += 1
                break
        d += c


    if pick_b == pick_d == 1:
        print(min(b, d))

    if not_pick_b == not_pick_d == 1:
        print('-1')

C++ Code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define pll pair<lli,lli>
#define pb push_back

#define MAX 100

lli a,b,c,d,dif;
map<lli,lli> mapa;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> a >> b >> c >> d;
    dif = d-b;
    while (true) {
        if (mapa[dif] == 1) {
            b = -1;
            break;
        }
        mapa[dif] = 1;

        if (dif == 0) break;
        if (dif < 0) {
            dif += c;
            d += c;
        }
        else if (dif > 0) {
            dif -= a;
            b += a;
        }
    }

    cout << b;

    return 0;
}

 	 	 							     	  	 		  		


Comments

Submit
0 Comments
More Questions

762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts
1166D - Cute Sequences
1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite
182B - Vasya's Calendar
934A - A Compatible Pair
1618F - Reverse
1684C - Column Swapping