数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
/*
数论/暴力:找出第一次到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的更多相关文章
- 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun
题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...
- 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 ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax
题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)
题意 n个值代表n个熊的高度 对于size为x的group strength值为这个group(连续的几个熊)中熊的最小的height值 对于x(1<=x<=n) 求出最大的strengt ...
- 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 ...
随机推荐
- 解压Zip
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import ja ...
- javascript模块化编程:CommonJS和AMD规范
AMD规范,异步模块定义.与CommonJS规范齐名并列. 作用都是利于JavaScript的模块化编程. 模块化编程的好处就是: 1.可重用 2.独立 3.能解决加载的依赖性问题 4.能解决重复加载 ...
- JSP JDBC 读取SQL Server 数据2
<%-- Created by IntelliJ IDEA. User: hellohongfu Date: 2017/12/21 Time: 0:16 To change this templ ...
- java 获取时间戳
//java 获取时间戳 long currentTime=System.currentTimeMillis();
- bzoj3727: PA2014 Final Zadanie
我真是SB之神呢这么SB的题都不会 肯定是先无脑正向思考,罗列下关系式: b[1]=∑a[i]*dep[i]=∑tot[i] (i!=1) b[i]=b[fa]-tot[i]+(tot[1]-tot[ ...
- EhCache+Redis实现分布式缓存
Ehcache集群模式 由于 EhCache 是进程中的缓存系统,一旦将应用部署在集群环境中,每一个节点维护各自的缓存数据,当某个节点对缓存数据进行更新,这些更新的数据无法在其它节点中共享,这不仅会降 ...
- 完美解决pip install scrapy,安装Scrapy错误:Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
1,在Python3.6 安装Scrapy 出现以下报错 2,错误分析 红色报的错误指向的是Twisted 1,Twisted 没安装上 2,Twisted 没安装成功 3,Twisted 版本与Py ...
- 一个小bug,关于fuse_mount_sys
在mount.c 中的 int fuse_mount_sys 函数中,如果注销掉 fd的open语句,此时fd一般为0. 然后,用普通用户运行ssfs且不加-f参数,一切显示正常 fuse_moun ...
- 书写优雅的shell脚本(五)- shell中(())双括号运算符
在使用shell的逻辑运算符"[]"使用时候,必须保证运算符与算数之间有空格. 四则运算也只能借助:let,expr等命令完成. 今天讲的双括号"(())"结构 ...
- CALayer和UIView
前言 本次分享将从以下方面进行展开: 曾被面试官问倒过的问题:层与视图的关系 CALayer类介绍及层与视图的关系 CAShapeLayer类介绍 UIBezierPath贝塞尔曲线讲解 CoreAn ...