题目传送门

 /*
数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围
t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束
详细解释:http://blog.csdn.net/u014357885/article/details/46044287
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std; typedef long long ll; const int MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f; int main(void) //Codeforces Round #305 (Div. 2) C. Mike and Frog
{
int m;
ll h1, a1, x1, y1, h2, a2, x2, y2;
while (scanf ("%d%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2) == )
{
ll t1 = -, t2 = -, p1 = -, p2 = -;
for (int i=; i<=m; ++i) //get t1, p1
{
h1 = (h1 * x1 + y1) % m;
if (h1 == a1) {t1 = i; break;}
}
if (t1 == -) {puts ("-1"); continue;}
for (int i=; i<=m; ++i)
{
h1 = (h1 * x1 + y1) % m;
if (h1 == a1) {p1 = i; break;}
} for (int i=; i<=m; ++i) //get t2, p2
{
h2 = (h2 * x2 + y2) % m;
if (h2 == a2) {t2 = i; break;}
}
if (t2 == -) {puts ("-1"); continue;}
for (int i=; i<=m; ++i)
{
h2 = (h2 * x2 + y2) % m;
if (h2 == a2) {p2 = i; break;}
} bool ok = false; //get t1 == t2
for (int i=; i<=*m; ++i)
{
if (t1 == t2) {ok = true; printf ("%I64d\n", t1); break;}
if (t1 < t2) t1 += p1;
else t2 += p2;
}
if (!ok) puts ("-1");
} return ;
} /*
5
4 2
1 1
0 1
2 3
1023
1 2
1 0
1 2
1 1
*/

数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog的更多相关文章

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

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

  2. 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/pr ...

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

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

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

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

  5. 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 ...

  6. 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 ...

  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(单调栈)

    题意 n个值代表n个熊的高度 对于size为x的group strength值为这个group(连续的几个熊)中熊的最小的height值 对于x(1<=x<=n) 求出最大的strengt ...

  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. Mac 上VitrualBox安装CentOS6.5 调整root分区的大小

    安装centOS的时候由于选择了动态调整磁盘大小.所以分配磁盘空间的时候就没多想,直接用的default的8G,以为不够了自己会调整,没想到是个大坑 发现提示空间不足的时候root仅仅有0k了.... ...

  2. iOS优化策略

    之前写的一系列文章或者小经验一直没有时间去整理放在博客上,今天整理出来,之前是写在作业部落,语法是markdown,点击链接浏览,仅供参考,希望对你有帮助. https://www.zybuluo.c ...

  3. HDU 6183 Color it cdq分治 + 线段树 + 状态压缩

    Color it Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Pro ...

  4. 数据库DCL、DDL、DML、DQL

    SQL三部分:data manipulation language      DCL: (控制)管理用户权限(GRANT.REVOKE),数据库整体配置      DDL: (定义)作用于数据库,表, ...

  5. YTU 2455: Pefect 数字

    2455: Pefect 数字 时间限制: 1 Sec  内存限制: 128 MB 提交: 749  解决: 146 题目描述 小明和小林做数字游戏,他们的游戏规则如下: 小明说出一个数字n,小林说出 ...

  6. HTTPS站点搭建教程:Win7/Windows Server 2008R2

    本文将由笔者为各位读者介绍在win7/windows server 2008R2环境下使用SSL加密协议建立WWW站点的全过程:https SSL证书安装的搭建以及本地测试环境. 要想成功架设SSL安 ...

  7. 【USACO】The Cow Prom

    [题目链接] 点击打开链接 [算法] tarjan求强连通分量 [代码] #include<bits/stdc++.h> #define MAXN 20005 using namespac ...

  8. CMDB资产采集笔记

    一.资产采集四种方式 1. Agent方式 API:Django接收数据并入库 程序:放置在每台服务器 应用场景:针对服务器较多的公司 步骤一: #执行本地命令的库 import subprocess ...

  9. Epoll简介以及例子

    第一部分:Epoll简介 问题 :  Select,Poll和Epoll的区别 答案 : Epoll和Select的区别 1. 遍历方式的区别.select判断是否有事件发生是遍历的,而epoll是事 ...

  10. PCB 线路铜皮面积(残铜率)计算的实现方法

    一个多月没更新博客园了,这里继续分享关于PCB工程相关一些知识,做过PCB工程都知道用使用genesis或incam是可以非常方便的计算得到铜皮面积这个参数[下图],但实际这个软件是通过什么算法计算出 ...