FLOYD判圈】的更多相关文章

题意: 有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 白书上的题 set,hash都占空间也不快 裸floyd判圈 洛谷U4984 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> u…
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operations with int64 type, all Delphi solutions for the problem 455 (Sequence analysis) run much slower than the same code written in C++ or Java. We do not gua…
大白书上P42那个计算器的题目就用到了这个办法,Floyd判圈法. 当然,用STL里的map也是可以的. #include <cstdio> #include <cmath> ; int n; int a[maxn], b[maxn], c[maxn]; void next(int a[]) { ; i < n - ; i++) a[i] = std::abs(a[i] - a[i + ]); a[n - ] = a[]; } bool equal(int a[], int…
CALCULATOR CONUNDRUM   Alice got a hold of an old calculator that can display n digits. She was bored enough to come up with the following time waster. She enters a number k then repeatedly squares it until the result overflows. When the result overf…
题意: 给两个数n, k,每次将k平方取k的前n位,问所有出现过的数的最大值 原来这就是floyd判圈法.. #include<cstdio> #include<cstdlib> #include<algorithm> #include<iostream> #include<cstring> #include<string> template<typename Q> Q read(Q& x) { static ch…
题意:有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 思路:这个题一定会出现循环,所以一个个模拟,遇到相同的就再之前所有数中找最大的输出即可. 怎么判断遇到相同的呢?如果装在数组里一一比较显然很慢,如果用v[k]判断k是否出现过,k范围太大空间不够用. 第一种方法是使用STL里的set来判重. #include<cstdio> #include<set> us…
Write an algorithm to determine if a number is "happy". 写出一个算法确定一个数是不是快乐数. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat th…
Floyd判圈算法 leetcode 上 编号为202 的happy number 问题,有点意思.happy number 的定义为: A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process u…
思路:利用的Floyd判圈,如果i能到j,j也能到i说明i和j在同一个圈里.每个人的名字可用map编号.最后DFS打印答案即可. AC代码 #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <utility> #include <string> #include <iostream> #include &l…
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈算法. 介绍一下floyd判圈算法:该算法适用于在线性时间复杂度内判断有限自动机.迭代函数.链表中是否有环,求环的起点(即链长)和环长. 可以先这么做:首先从起点S出发,给定两个指针,一个快指针一个慢指针,然后每次快指针走1步,慢指针走2步,直到相遇为止.如果已经到达终点/达到规定步数时仍然没有相遇…