【codeforces 787A】The Monster

【题目链接】:http://codeforces.com/contest/787/problem/A

【题意】

把b一直加a->得到x
把d一直加c->得到y
然后问你x和y可不可能有相同的值.
有的话,输出那个最小的;

【题解】

等价于
令t=(b+u*a-d)%c==0
u为整数
这里如果b< d就swap(a,c),swap(b,d)就好;
然后如果t遇到了重复的值,就结束,往后都不可能了;
如果t中途变成0了,就输出那个u;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;

map <int, int> dic;
int a, b, c, d;
int ans = -1;

void in()
{
    rei(a), rei(b);
    rei(c), rei(d);
}

int get_ans()
{
    int x = 0;
    if (b < d)
    {
        swap(a, c);
        swap(b, d);
    }
    //b>=d
    int t = (b + x*a - d) % c;
    while (t != 0)
    {
        if (dic[t])
            return -1;
        dic[t] = 1;
        x++;
        t = (b + x*a - d) % c;
    }
    return x;
}

int main()
{
    //freopen("F:\\rush.txt", "r", stdin);
    in();
    ans = get_ans();
    if (ans==-1)
        puts("-1");
    else
        printf("%d\n",b+ans*a);
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
posted @ 2017-10-04 18:45  AWCXV  阅读(180)  评论(0编辑  收藏  举报