zoukankan      html  css  js  c++  java
  • Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力

     A. Mike and Frog

    Time Limit: 20 Sec  Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/547/problem/A

    Description

    Mike has a frog and a flower. His frog is named Xaniar and his flower is named Abol. Initially(at time 0), height of Xaniar is h1 and height of Abol is h2. Each second, Mike waters Abol and Xaniar.

    So, if height of Xaniar is h1 and height of Abol is h2, after one second height of Xaniar will become and height of Abol will become where x1, y1, x2 and y2 are some integer numbers and denotes the remainder of a modulo b.

    Mike is a competitive programmer fan. He wants to know the minimum time it takes until height of Xania is a1 and height of Abol is a2.

    Mike has asked you for your help. Calculate the minimum time or say it will never happen.

    Input

    The first line of input contains integer m (2 ≤ m ≤ 106).

    The second line of input contains integers h1 and a1 (0 ≤ h1, a1 < m).

    The third line of input contains integers x1 and y1 (0 ≤ x1, y1 < m).

    The fourth line of input contains integers h2 and a2 (0 ≤ h2, a2 < m).

    The fifth line of input contains integers x2 and y2 (0 ≤ x2, y2 < m).

    It is guaranteed that h1 ≠ a1 and h2 ≠ a2.

    Output

    Print the minimum number of seconds until Xaniar reaches height a1 and Abol reaches height a2 or print -1 otherwise.

    Sample Input

    5
    4 2
    1 1
    0 1
    2 3

    Sample Output

    3

    HINT

    题意

    h1=(h1*x1+y1)%m,h2=(h2*x2+y2)%m,求最短时间,h1到达a1的同时h2到达a2

    题解:

    暴力美学,首先先跑一法,打表出h1到达a1的时间,打表出h2到达a2的时间

    首先,我们因为循环2*m次,根据抽屉原理,如果有一次达到了a1,那么必然会有第二次到达a1

    然后ans1[0]储存的是h1到达a1的时间,ans2[0]储存的是h2到达a2的时间

    然后周期显然就是ans1[1]-ans1[0]和ans2[1]-ans2[0]

    然后暴力找就好了!

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)  
    #define maxn 200001
    #define mod 1000000007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    vector<int> ans1;
    vector<int> ans2;
    int main()
    {
        //test;
        ll m,h1,h2,a1,a2,x1,x2,y1,y2;
        m=read();
        h1=read(),a1=read();
        x1=read(),y1=read();
        h2=read(),a2=read();
        x2=read(),y2=read();
        
        int tot=0;
        while(tot<=2*m)
        {
            if(h1==a1)
                ans1.push_back(tot);
            if(h2==a2)
                ans2.push_back(tot);
            tot++;
            h1=(x1*h1+y1)%m;
            h2=(x2*h2+y2)%m;
        }
        if(ans1.empty()||ans2.empty())
        {
            cout<<"-1"<<endl;
            return 0;
        }
        ll t1=ans1[0],t2=ans2[0];
        ll add1=ans1[1]-ans1[0],add2=ans2[1]-ans2[0];
        for(int i=0;i<5000000;i++)
        {
            if(t1==t2)
            {
                printf("%lld
    ",t1);
                return 0;
            }
            if(t1<t2)
                t1+=add1;
            else
                t2+=add2;
        }
        cout<<"-1"<<endl;
        return 0;
    }
  • 相关阅读:
    javascript 犀牛书
    Hmtl/css
    SQL Server 查询中使用Union或Union All后Order by排序无效(嵌套查询乱序)
    CSS 动画
    CSS 文字排版
    CSS 太极图
    Amazon Products 服务费估算
    CSS3 结构伪类选择器
    JS 执行机制
    vue 生命周期函数的学习
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4533060.html
Copyright © 2011-2022 走看看