今天写线段树写到要用到这个算法的题目,简单的学习一下。

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判环算法的更多相关文章

  1. 【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 ...

  2. Floyed判环/龟兔算法

    求[(5+2√6)2^x+1 ] mod p 的值,其中 0 ≤ x < 232 , p 是个质数,p ≤ 46337 .(这里介绍的是一种暴力的做法) (5+2√6)2^n+1 = an + ...

  3. floyd判环算法(龟兔赛跑算法)

    floyd判环算法(龟兔赛跑算法) 注意,这个算法是用来判断一条链+一条环的图,环的长度或者环与链的交界处的,所以此floyd非彼floyd(虽然都是一个人想出来的). (图不是我的) 如果只要求环的 ...

  4. 《Java算法》判重算法-整数判重

    判重算法-整数判重 /** * 判断大于1,小于63的整数是否出现重复数字. * * 算法逻辑:先获取8 根据移位(1 << arrInt[i]) 得到2进制数100000000 , * ...

  5. leetcode202(Floyd判圈算法(龟兔赛跑算法))

    Write an algorithm to determine if a number is "happy". 写出一个算法确定一个数是不是快乐数. A happy number ...

  6. spfa算法及判负环详解

    spfa     (Shortest Path Faster Algorithm) 是一种单源最短路径的算法,基于Bellman-Ford算法上由队列优化实现. 什么是Bellman_Ford,百度内 ...

  7. Floyd判圈算法

    Floyd判圈算法 leetcode 上 编号为202 的happy number 问题,有点意思.happy number 的定义为: A happy number is a number defi ...

  8. Floyd判断环算法总结

    Floyd判断环算法 全名Floyd’s cycle detection Algorithm, 又叫龟兔赛跑算法(Floyd's Tortoise and Hare),常用于链表.数组转化成链表的题目 ...

  9. Floyd 判圈算法

    Floyd 判圈算法 摘自维基百科, LeetCode 上 141题 Linked List Cycle 用到这个, 觉得很有意思. 记录一下. 链接: https://zh.wikipedia.or ...

随机推荐

  1. 给学妹的 Java 学习路线

    大家好,这篇文章主要是讲解下如何自学 Java,这个问题有很多粉丝私信问过,今天又有直系学妹问我如何学习 Java? 我就以我的经历,总结下分享给大家,有不当指出或者有更好的方法建议也欢迎留言指出,大 ...

  2. 第十三节:telnetlib、redis、threading模块

    telnetlib模块案例: import telnetlib,re class TelnetInfo(): def telnetdo(self, host, port, command): tn = ...

  3. Delphi TMemo 可以显示、编辑多行文本

    多行编辑框组件(TMemo)TMemo组件可以显示.编辑多行文本,是一个标准的Windows多行编辑组件.对一些比较多的文本内容可以利用TMemo组件来显示.编辑. 1.TMemo组件的典型用法 TM ...

  4. vue中的错误日志

    一.Error compiling template: Component template requires a root element, rather than just text. 这个错误意 ...

  5. 一道简单的SQL注入题

    这是我真正意义上来说做的第一道SQL题目,感觉从这个题目里还是能学到好多东西的,这里记录一下这个题目的writeup和在其中学到的东西 link:https://www.ichunqiu.com/ba ...

  6. mybatis源码配置文件解析之二:解析settings标签

    在前边的博客中分析了mybatis解析properties标签,<mybatis源码配置文件解析之一:解析properties标签>.下面来看解析settings标签的过程. 一.概述 在 ...

  7. 详解 DatagramSocket类

    (请观看本人博文 -- <详解 网络编程>) DatagramSocket 概述: 这类代表一个发送和接收数据包的插座. 该类是遵循 UDP协议 实现的一个Socket类. 数据报套接字发 ...

  8. Oracle Database 12C 安装教程

    一.下载地址 Oracle Database 官方下载地址:https://www.oracle.com/database/technologies/oracle-database-software- ...

  9. Python软件定时器APScheduler使用【软件定时器,非操作系统定时器,软件可控的定时器】【用途:定时同步数据库和缓存等】【刘新宇】

    APScheduler使用 APScheduler (advanceded python scheduler)是一款Python开发的定时任务工具. 文档地址 https://apscheduler. ...

  10. 关于synergy的问题

    报错信息主要集中在以下两条: ERROR: ssl error occurred (system call failure) ERROR: eof violates ssl protocol 通过查找 ...