龟兔赛跑算法 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 ...
随机推荐
- 类 文件 右下角呈现 红色小圆圈,里面有一个J 标记
intellj(idea) 项目中类 文件 右下角呈现 红色小圆圈,里面有一个J 标记,表明此为 未设置为源文件,没有编译,本来应该是属于源文件的,结果现在没有被标记为源文件,也就没法编译了.
- list[列表]的使用
#!/usr/bin/env python3# -*- coding:utf-8 -*-# name:zzyushop_list = [["手机",5000], ["电脑 ...
- vue 全局自定义组件
1.vue文件 <template> <div style="position: absolute;bottom: 10px;text-align: center;widt ...
- O - Navigation System CodeForces - 1321D
题目大意:有一个导航系统,会根据你当前的位置,规划到目的地的最短路线,给你一个有向图,和一条行驶路径,问你导航重新规划路径的最大次数和最小次数. 读题的时候题意特别不理解,何为最大次数,何为最小次数? ...
- G - Messy codeforces1262C
题目大意: 输入n和m,n是n个字符,m是m个前缀.对前缀的规定可以配对的括号.比如(),,((()))等等.在输入n个括号字符,对这个n个字符,通过交换使其满足m个前缀.交换次数不限,规则想当与re ...
- [GO] mac安装GO 初次尝试
其实试了好多方法,我用的是下面这种方法,简单快捷! 安装homebrew 在终端输入命令 ruby -e "$(curl -fsSL https://raw.githubuserconten ...
- 尝试用python开发一款图片压缩工具1:尝试 pillow库
开发目的 我经常使用图片.公众号文章发文也好,还是生活中要使用素材.图片是一种比文字更加直观的载体.但是图片更加占用带宽,很多软件都对图片有大小限制.图片太大也会影响加载速度.我试过几款图片压缩工具, ...
- java IO流 之 字节流与字符流
其实学习了file文件基础类,后面的字节流和字符流都特别简单了,首先需要知道字节流和字符流的区别 字节流: 用来传送图片.各种文件.大文件.文本都是通过字节流进行传输的. 字符流: 只能读取文本信息 ...
- 基于Neo4j的个性化Pagerank算法文章推荐系统实践
新版的Neo4j图形算法库(algo)中增加了个性化Pagerank的支持,我一直想找个有意思的应用来验证一下此算法效果.最近我看Peter Lofgren的一篇论文<高效个性化Pagerank ...
- Sublime text 3快捷键壁纸版