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[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
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=;
while(tot<=*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 ;
}
ll t1=ans1[],t2=ans2[];
ll add1=ans1[]-ans1[],add2=ans2[]-ans2[];
for(int i=;i<;i++)
{
if(t1==t2)
{
printf("%lld\n",t1);
return ;
}
if(t1<t2)
t1+=add1;
else
t2+=add2;
}
cout<<"-1"<<endl;
return ;
}

Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力的更多相关文章

  1. 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

    题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...

  2. Codeforces Round #305 (Div. 2) B. Mike and Fun 暴力

     B. Mike and Fun Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...

  3. Codeforces Round #305 (Div. 2) A. Mike and Fax 暴力回文串

     A. Mike and Fax Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/548/pro ...

  4. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

  5. 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun

    题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...

  6. 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax

    题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...

  7. Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈

    B. Mike and Feet Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...

  8. Codeforces Round #305 (Div. 2) D. Mike and Feet 单调栈

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  9. Codeforces Round #305 (Div. 2) D. Mike and Feet

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. MongoDB之数据库命令操作(二)

    现在详细学习一下mongodb的数据库操作. 查询语句 db.xxx(集合name).find() # 查询 db.xxx(集合name).findOne() # 只返回一个 db.xxx(集合nam ...

  2. dlmalloc(一)【转】

    转自:http://blog.csdn.net/ycnian/article/details/12971863 我们写过很多C程序了,经常会分配内存.记得刚学C语言时老师说过,可以向两个地方申请内存: ...

  3. python3.x的HTMLTestRunner.py文件

    """A TestRunner for use with the Python unit testing framework. Itgenerates a HTML re ...

  4. python 面试

    知识总结 面试(一)

  5. POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)

    题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...

  6. MVC – 9.mvc整体请求流程

    1.请求管道 2~5微软自己的验证,我们一般不用. 在全局配置文件中-已经配置一个路由过滤器-为第7个事件注册了路由方法 1.在application_start中向静态路由表注册了路由数据,在管道第 ...

  7. 使用php扩展mcrypt实现AES加密

    AES(Advanced Encryption Standard,高级加密标准)是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用.Rijndael是 ...

  8. fastdfs5.11+centos7.2 按照部署(一)【转载】

    1.绪论 最近要用到fastDFS,所以自己研究了一下,在搭建FastDFS的过程中遇到过很多的问题,为了能帮忙到以后搭建FastDFS的同学,少走弯路,与大家分享一下.FastDFS的作者淘宝资深架 ...

  9. Wannafly挑战赛18 B - 随机数

    思路:化简公式,Pn 表示 进行n 次操作,有奇数次1的概率 Pn = (1 - x) * Pn - 1  + x * (1 - Pn - 1) 得通项公式 Pn = (1 - (1 - 2 * x) ...

  10. live555例子程序编译连接时发现函数未定义问题

    1 调整连接库的顺序. 2 更新头文件与所用的库一致