• 【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
    ",b+ans*a);
        //printf("
    %.2lf sec 
    ", (double)clock() / CLOCKS_PER_SEC);
        return 0;
    }
  • 相关阅读:
    win7下virtualbox遇到的问题
    2.5年, 从0到阿里
    TCP/IP入门(4) --应用层
    TCP/IP入门(3) --传输层
    TCP/IP入门(2) --网络层
    TCP/IP入门(1) --链路层
    Socket编程实践(13) --UNIX域协议
    Socket编程实践(12) --UDP编程基础
    Socket编程实践(10) --select的限制与poll的使用
    Socket编程实践(9) --套接字IO超时设置方法
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626532.html
Copyright © 2020-2023  润新知