zoukankan      html  css  js  c++  java
  • C

    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.

    Examples
    Input
    5
    4 2
    1 1
    0 1
    2 3
    Output
    3
    Input
    1023
    1 2
    1 0
    1 2
    1 1
    Output
    -1
    Note
    In the first sample, heights sequences are following:

    Xaniar:

    Abol:

    思路:待补

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main()
    {
        long long m,h1,a1,x1,y1,h2,a2,x2,y2;
        scanf("%lld %lld %lld %lld %lld",&m,&h1,&a1,&x1,&y1);
        scanf("%lld %lld %lld %lld",&h2,&a2,&x2,&y2);
        vector<int>ans1,ans2;
        int cnt = 0;
        while(cnt <= 2 * m)//暴力循环节,至于为什么是2 * m次,还真的没有搞懂,留坑待补。
        {
            if(h1 == a1)
                ans1.push_back(cnt);
            if(h2 == a2)
                ans2.push_back(cnt);
            cnt++;
            h1 = (x1 * h1 + y1) % m;
            h2 = (x2 * h2 + y2) % m;
        }
        if(ans1.empty() || ans2.empty())
        {
            printf("-1
    ");
            return 0;
        }
        long long t1 = ans1[0];
        long long t2 = ans2[0];
    
        long long s1 = ans1[1] - ans1[0];//因为a1达到h1的过程是循环的,所以ans[1] - ans[0]可以作为整个过程的次数。
        long long s2 = ans2[1] - ans2[0];
        for(int i = 1;i < 5e6;i++)
        {
            if(t1 == t2)
            {
                printf("%lld",t1);
                return 0;
            }
            if(t1 < t2)
            {
                t1 += s1;
            }
            else if(t1 > t2)
            {
                t2 += s2;
            }
        }
        printf("-1
    ");
        return 0;
    }
    
    
  • 相关阅读:
    Spring Annotation注解进行aop的学习
    使用ADO读取SQL数据库
    GetInventTable组装SQL语句使用通配符
    获取当前用户所属的所有仓位,DictRelation,
    UnitConvert,单位换算,单位转换
    数字货币书写转英文货币书写
    导出xml
    存储过程IN参数疑难问题解决方法
    TempTable 临时表
    多栏报表 多列报表
  • 原文地址:https://www.cnblogs.com/tomjobs/p/10617579.html
Copyright © 2011-2022 走看看