龟兔赛跑算法 floyed判环算法
今天写线段树写到要用到这个算法的题目,简单的学习一下。
https://blog.csdn.net/javaisnotgood/article/details/89243876
https://blog.csdn.net/wall_f/article/details/8780209
https://blog.csdn.net/qq_37025443/article/details/88852318
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<set>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<list>
#include <queue>
#include <map>
#include <string>
#include <fstream>
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 2018;
int mp[N]; PII cal(int s) {
int fast = s;
int slow = s;
bool isCircular = true; do {
if (mp[fast] == -1 || mp[mp[fast]] == -1) {
isCircular = false;
break;
} fast = mp[mp[fast]];
slow = mp[slow];
} while (fast != slow);
//确定起点
slow = s;
while (slow != fast) {
slow = mp[slow];
fast = mp[fast];
} //环的长度
int i = 0;
do {
slow = mp[slow];
i++;
} while (slow != fast);
//printf("环的起点:%d\n环的长度:%d\n",slow,i);
return make_pair(slow, i);
} int main() {
for (int i = 0; i < 2018; i++) {
mp[i] = i * i % 2018;
}
int maxstep = 0;
int maxcir = 0;
for (int i = 0; i < 2018; i++) {
PII tmp = cal(i);
maxcir = max(tmp.second, maxcir);
int j = 0;
int x = i;
while (x != tmp.first) {
x = mp[x];
j++;
}
maxstep = max(maxstep, j);
}
printf("最大环:%d\n最大入环距离:%d\n", maxcir, maxstep);
}
龟兔赛跑算法 floyed判环算法的更多相关文章
- 【set&&sstream||floyed判环算法】【UVa 11549】Calculator Conundrum
CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bored ...
- Floyed判环/龟兔算法
求[(5+2√6)2^x+1 ] mod p 的值,其中 0 ≤ x < 232 , p 是个质数,p ≤ 46337 .(这里介绍的是一种暴力的做法) (5+2√6)2^n+1 = an + ...
- floyd判环算法(龟兔赛跑算法)
floyd判环算法(龟兔赛跑算法) 注意,这个算法是用来判断一条链+一条环的图,环的长度或者环与链的交界处的,所以此floyd非彼floyd(虽然都是一个人想出来的). (图不是我的) 如果只要求环的 ...
- 《Java算法》判重算法-整数判重
判重算法-整数判重 /** * 判断大于1,小于63的整数是否出现重复数字. * * 算法逻辑:先获取8 根据移位(1 << arrInt[i]) 得到2进制数100000000 , * ...
- leetcode202(Floyd判圈算法(龟兔赛跑算法))
Write an algorithm to determine if a number is "happy". 写出一个算法确定一个数是不是快乐数. A happy number ...
- spfa算法及判负环详解
spfa (Shortest Path Faster Algorithm) 是一种单源最短路径的算法,基于Bellman-Ford算法上由队列优化实现. 什么是Bellman_Ford,百度内 ...
- Floyd判圈算法
Floyd判圈算法 leetcode 上 编号为202 的happy number 问题,有点意思.happy number 的定义为: A happy number is a number defi ...
- Floyd判断环算法总结
Floyd判断环算法 全名Floyd’s cycle detection Algorithm, 又叫龟兔赛跑算法(Floyd's Tortoise and Hare),常用于链表.数组转化成链表的题目 ...
- Floyd 判圈算法
Floyd 判圈算法 摘自维基百科, LeetCode 上 141题 Linked List Cycle 用到这个, 觉得很有意思. 记录一下. 链接: https://zh.wikipedia.or ...
随机推荐
- PHP 语法引用使用及实现
说明 这里基于 php7.2.5 进行测试,php7 之后内部结构变化应该不是太大,但与 php5.X 有差别. 什么是引用 在 PHP 中引用是一种数据类型 (结构),是指 指向同一个类型的数据结构 ...
- cxx signal信号捕获
kill -9 [pid] 该信号不能被捕获 #include <iostream> #include <csignal> static void vSignalHandler ...
- group_concat有长度限制
group_concat有长度限制 group_concat 详细用法请点此链接. group_concat有长度限制!长度陷阱用了group_concat后,select里如果使用了li ...
- pytorch cheatsheet
- 嘿嘿,我就知道面试官接下来要问我 ConcurrentHashMap 底层原理了,看我怎么秀他
前言 上篇文章介绍了 HashMap 源码后,在博客平台广受好评,让本来己经不打算更新这个系列的我,仿佛被打了一顿鸡血.真的,被读者认可的感觉,就是这么奇妙. 然后,有读者希望我能出一版 Concur ...
- Vmware下安装Linux
Linux系统 开源的操作系统.主要是应用在软件的服务器,性能比windows要好. Linux系统(ubuntu,centos,redhat,aix....) Linux主要是通过命令去操作计算机, ...
- Java 8 到 Java 14,改变了哪些你写代码的方式?
前几天,JDK 14 正式发布了,这次发布的新版本一共包含了16个新的特性. 其实,从Java8 到 Java14 ,真正的改变了程序员写代码的方式的特性并不多,我们这篇文章就来看一下都有哪些. La ...
- jmeter插件 --PerfMon Metrics Collector监控工具的使用
PerfMon Metrics Collector 用来监控 被压测服务器的cpu.内存.磁盘.网络等 1.服务端监控程序ServerAgent下载 https://github.com/undera ...
- 技术周刊 · 0202 年了,你还不学一下 WASM 么?
蒲公英 · JELLY技术周刊 Vol.04 「蒲公英」期刊全新升级--JELLY技术周刊!深度挖掘业界热点动态,来自团队大咖的专业点评,带你深入了解团队研究的技术方向. 登高远眺 天高地迥,觉宇宙之 ...
- php +go关键字实现协程
来源: https://studygolang.com/articles/17631?fr=sidebar 今天在知乎浏览时忽然发现了一个有趣的东西,php竟然可以实现协程的实现,而且还是通过go关键 ...