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

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. floyd最小环&&模板

    floyd的核心代码: ;k<=n;k++){ ;i<=n;i++){ ;j<=n;j++){ dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j] ...

  2. Visual C++ 6.0踩坑记录---在Win10下安装Visual C++ 6.0安装成功后点击“打开”按钮闪退问题

    前言: 为了更好的学习C及C++,前段时间下载了Microsoft Visual C++ 6.0(以下简称VC6),原因是VC6具有查看反汇编代码.监视内存.寄存器等功能,并且因为本人正在学习滴水逆向 ...

  3. 讲讲HashMap的理解,以及HashMap在1.7和1.8版本的变化(2020/4/16)

    HashMap的适用场景,作用,优缺点

  4. 学习笔记分享之汇编---3. 堆栈&标志寄存器

    前言:   此文章收录在本人的<学习笔记分享>分类中,此分类记录本人的学习心得体会,现全部分享出来希望和大家共同交流学习成长.附上分类链接:   https://www.cnblogs.c ...

  5. shell脚本知识

    1.提示符变量PS1 修改提示符变量:PS1="[u\@\h \t \w]" 修改环境变量设置文件bash_profile需要使用source或者.加上该文件使之生效 位置参数从1 ...

  6. L16 LeNet

    **本小节用到的数据下载 1.涉及语句 import d2lzh1981 as d2l 数据1 : d2lzh1981 链接:https://pan.baidu.com/s/1LyaZ84Q4M75G ...

  7. HTTPoxy漏洞(CVE-2016-5385)复现记录

    漏洞介绍: httpoxy是cgi中的一个环境变量:而服务器和CGI程序之间通信,一般是通过进程的环境变量和管道. CGI介绍 CGI 目前由 NCSA 维护,NCSA 定义 CGI 如下:CGI(C ...

  8. Shellshock远程命令注入(CVE-2014-6271)漏洞复现

    请勿用于非法用法,本帖仅为学习记录 shelshocke简介: shellshock即unix 系统下的bash shell的一个漏洞,Bash 4.3以及之前的版本在处理某些构造的环境变量时存在安全 ...

  9. .NET 4 实践 - 使用dynamic和MEF实现轻量级的AOP组件 (4)

    转摘 https://www.cnblogs.com/niceWk/archive/2010/07/23/1783394.html 借花献佛 前面我们介绍了构成DynamicAspect绝大部分的类, ...

  10. MVC-过滤器-权限认证

    过滤器主要基于特性,aop来实现对MVC管道中插入其他处理逻辑.比如,访问网站,需要检查是否已经登陆,若没登陆跳入登陆界面. 样例: 方法注册 执行效果 当不符合认证时: 上面是方法注册特性.还有类注 ...