码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces548C:Mike and Frog

时间:2015-06-12 19:30:41      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:codeforces

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 amodulo 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 test(s)
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: 技术分享


题意:

给出公式(h*x+y)%m,然后分别给出两个物体的a,h,x,y,问通过公式,两个物体能否同时到达a1和a2


思路:

本题的关键还是在于求循环节


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 200005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

int main()
{
    LL flag = 0;
    LL m,h1,h2,a1,a2,x1,x2,y1,y2;
    LL i,j,k;
    LL p1,r1,p2,r2;
    p1 = p2 = r1 = r2 = -1;
    scanf("%I64d",&m);
    scanf("%I64d%I64d%I64d%I64d",&h1,&a1,&x1,&y1);
    scanf("%I64d%I64d%I64d%I64d",&h2,&a2,&x2,&y2);
    for(i = 1; i<=2*m; i++)
    {
        h1 = (h1*x1+y1)%m;
        if(h1 == a1)
        {
            if(p1 == -1)
                p1 = i;//起点
            else if(r1 == -1)
                r1 = i-p1;//循环长度
        }
        h2 = (h2*x2+y2)%m;
        if(h2 == a2)
        {
            if(p2 == -1)
                p2 = i;
            else if(r2 == -1)
                r2 = i-p2;
        }
    }
    if(p1 == -1 || p2 == -1)
        printf("-1\n");
    else if(p1 == p2)
        printf("%I64d\n",p1);
    else
    {
        for(i = 1; i<=2*m; i++)//因为循环节最长也不过m,所以枚举2*m就
        {
            if(p1<p2)
                p1+=r1;
            else
                p2+=r2;
            if(p1==p2)
            {
                printf("%I64d\n",p1);
                return 0;
            }
        }
        printf("-1\n");
    }

    return 0;
}


Codeforces548C:Mike and Frog

标签:codeforces

原文地址:http://blog.csdn.net/libin56842/article/details/46473637

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!