codeforces #305 A Mike and Frog
挺简单的题目,但是有一堆恶心的边界
在刨去恶心的边界之后:
假定我们知道两边的循环节为b1,b2
其中h第一次到达目标的时间为a1,a2
又知道对于答案t
t=a1+b1*t1=a2+b2*t2
不妨枚举t1,判断是否存在可行解即可
又因为LCM(b1,b2)就开始循环了
且b1*b2<=b1*mod
所以我们枚举t1的范围在[0,mod]即可
如果在这个范围内无解,则一定无解
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long LL;
bool vis[1000010];
int mod,h,a,x,y;
int Go(int h,int x,int y){
memset(vis,0,sizeof(vis));
int cnt=0;
while(1){
if(vis[h])return -1;
vis[h]=1;
h=(1LL*h*x+y)%mod;
++cnt;
if(h==a)return cnt;
}
} int main(){
scanf("%d",&mod);
scanf("%d%d%d%d",&h,&a,&x,&y);
int a1=Go(h,x,y),b1=Go(a,x,y);
scanf("%d%d%d%d",&h,&a,&x,&y);
int a2=Go(h,x,y),b2=Go(a,x,y);
if(a1==-1||a2==-1){printf("-1\n");return 0;}
if(b1==-1&&b2==-1){
if(a1==a2)printf("%d\n",a1);
else printf("-1\n");
return 0;
}
if(b1!=-1&&b2!=-1){
for(int i=0;i<=mod;++i){
if(a1+1LL*b1*i>=a2&&(a1+1LL*b1*i-a2)%b2==0){
cout<<a1+1LL*b1*i<<endl;
return 0;
}
}printf("-1\n");
return 0;
}else{
if(b1==-1)swap(a1,a2),swap(b1,b2);
if(a2>=a1&&(a2-a1)%b1==0)printf("%d\n",a2);
else printf("-1\n");
}return 0;
}
codeforces #305 A Mike and Frog的更多相关文章
- codeforces #305 B Mike and Feet
跟之前做过的51Nod的移数博弈是一样的QAQ 我们考虑每个数的贡献 定义其左边第一个比他小的数的位置为L 定义其右边第一个比他小的数的位置为R 这个可以用排序+链表 或者 单调队列 搞定 那么对于区 ...
- codeforces #305 D Mike and Fish
正解貌似是大暴搜? 首先我们考虑这是一个二分图,建立网络流模型后很容易得出一个算法 S->行 容量为Num[X]/2; 行->列 容量为1 且要求(x,y)这个点存在 列->T 容量 ...
- codeforces #305 C Mike and Foam
首先我们注意到ai<=50w 因为2*3*5*7*11*13*17=510510 所以其最多含有6个质因子 我们将每个数的贡献分离, 添加就等于加上了跟这个数相关的互素对 删除就等于减去了跟这个 ...
- codeforces #305 E Mike and friends
原问题可以转化为:给定第k个字符串,求它在L-R的字符串里作为子串出现了多少次 定义子串为字符串的某个前缀的某个后缀(废话) 等价于我们把一个字符串插入到trie里,其过程中每个经过的节点和其向上的f ...
- 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog
题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...
- 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 ...
- CF #305 (Div. 2) C. Mike and Frog(扩展欧几里得&&当然暴力is also no problem)
C. Mike and Frog time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces 547C/548E - Mike and Foam 题解
目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...
- codeforces 547A Mike and Frog
近期都是这样的题呢. . .... 哎 開始想纯暴力(体如今跳出循环t>=那里.,,,)..,.随着数据变大.. ...(t=499981500166是能够的),,,..,,23333333 超 ...
随机推荐
- JQuery验证工具
一.写法一 var Validator = { // 邮箱isEmail : function(s) {var p = "^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z ...
- UEditor上传图片等附件都出现红叉
我的环境: vs2010+netframework3.5+ueditor1_3_5-utf8-net 问题:UEditor上传图片等附件都出现红叉 尝试了网络上各种方法都不对, 后来只能自己看下ima ...
- (转) DockPanel 右键增加关闭,除此之外全部关闭的功能
在项目中新建一个class文件,代码如下: using System; using System.Collections.Generic; using System.ComponentModel; u ...
- Php 操作事务
PHP来操作数据库 关于事务操作 连接数据 mysql_connect('localhost','root','123'); 设置字符集 mysql_query('set names utf8'); ...
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- 致vi老大 2011.1
文/安然 亲爱的,你即将离去 飞机起飞的一刻, 请珍藏起我们2010的回忆 桃源仙谷,曾留下我们踏青的足迹 难忘,石头上小憩 小北门外,我们在大排档里尽情欢喜 见证,杯盘狼藉 饺子店,是冬日里四面八方 ...
- java学习笔记_GUI(5)
demo如何为不同的button创建对应的响应函数 import javax.swing.*; import java.awt.event.*; import java.awt.*; class My ...
- JS滑动门,JQuery滑动门
<a href="#" id="one1" onmouseover="setTab('one',1,2)" class="h ...
- webpack+react+jquery和jquery插件
要引入jquery插件 全局引入jquery plugins : [new webpack.ProvidePlugin({ $: 'jquery', jQuery:'jquery' "win ...
- wordpress在Linux nginx下权限设置
1.wordpress 权限对安装和使用效果的影响很大:权限错误将影响theme的安装:不能安装theme或者修改theme或删除theme. 相关设置: chmod 755 wordpress f ...