UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)
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 overflows, only the n most significant digits are displayed on the screen and an error flag appears. Alice can clear the error and continue squaring the displayed number. She got bored by this soon enough, but wondered:
“Given n and k, what is the largest number I can get by wasting time in this manner?”
Program Input
The first line of the input contains an integer t (1 ≤ t ≤ 200), the number of test cases. Each test case contains two integers n (1 ≤ n ≤ 9) and k (0 ≤ k < 10n) where n is the number of digits this calculator can display k is the starting number.
Program Output
For each test case, print the maximum number that Alice can get by repeatedly squaring the starting number as described.
Sample Input & Output
INPUT
2
1 6
2 99
OUTPUT
9
99 题目大意:计算器谜题。有一个老式计算器,只能显示n位数字。有一天,你无聊了,于是输入一个整数k,然后反复平方,直到溢出。每次溢出时,计算器会显示出结果的最高n位和一个错误标记。然后清除错误标记,继续平方。如果一直这样做下去,能得到的最大数是多少?比如,当n=1,k=6时,计算器将以此显示6、3(36的最高位),9、8(81的最高位),6(64的最高位),3... 分析:题目已经暗示了计算器显示出的数将出现循环。
想象一下,假设两个小孩在一个“可以无限向前跑”的跑道上赛跑,同时出发,但其中一个小孩的速度是另一个的2倍。如果跑道是直的,跑得快的小孩永远在前面;但如果跑道有环,则跑得快的小孩将“追上”跑得慢的小孩。
这个算法称为Floyd判圈算法,不仅空间复杂度降为O(1),运行时间也将缩短到0.5秒。 代码如下:
#include<iostream>
using namespace std; int buf[]; int next(int n, int k) {
if(!k) return ;
long long k2 = (long long)k * k;
int L = ;
while(k2 > ) { buf[L++] = k2 % ; k2 /= ; } // 分离并保存k2的各个数字
if(n > L) n = L;
int ans = ;
for(int i = ; i < n; i++) // 把前min{n,L}位重新组合
ans = ans * + buf[--L];
return ans;
} int main() {
int T;
cin >> T;
while(T--) {
int n, k;
cin >> n >> k;
int ans = k;
int k1 = k, k2 = k;
do {
k1 = next(n, k1); // 小孩1
k2 = next(n, k2); if(k2 > ans) ans = k2; // 小孩2,第一步
k2 = next(n, k2); if(k2 > ans) ans = k2; // 小孩2,第二步
} while(k1 != k2); // 追上以后才停止
cout << ans << endl;
}
return ;
}
UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)的更多相关文章
- UVA 11549 Calculator Conundrum (Floyd判圈算法)
题意:有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 思路:这个题一定会出现 ...
- Floyd判圈算法 UVA 11549 - Calculator Conundrum
题意:给定一个数k,每次计算k的平方,然后截取最高的n位,然后不断重复这两个步骤,问这样可以得到的最大的数是多少? Floyd判圈算法:这个算法用在循环问题中,例如这个题目中,在不断重复中,一定有一个 ...
- UVa 11549 计算器谜题(Floyd判圈算法)
https://vjudge.net/problem/UVA-11549 题意: 有一个老式计算器,只能显示n位数字,输入一个整数k,然后反复平方,如果溢出的话,计算器会显示结果的最高n位.如果一直这 ...
- SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...
- leetcode202(Floyd判圈算法(龟兔赛跑算法))
Write an algorithm to determine if a number is "happy". 写出一个算法确定一个数是不是快乐数. A happy number ...
- Floyd判圈算法
Floyd判圈算法 leetcode 上 编号为202 的happy number 问题,有点意思.happy number 的定义为: A happy number is a number defi ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Floyd 判圈算法
Floyd 判圈算法 摘自维基百科, LeetCode 上 141题 Linked List Cycle 用到这个, 觉得很有意思. 记录一下. 链接: https://zh.wikipedia.or ...
- Floyd判圈算法 Floyd Cycle Detection Algorithm
2018-01-13 20:55:56 Floyd判圈算法(Floyd Cycle Detection Algorithm),又称龟兔赛跑算法(Tortoise and Hare Algorithm) ...
随机推荐
- linux Grant 添加 MySql 用户
Grant 添加 MySql 用户 2009-04-03 14:40 我安装的版本: mysql> select version();+------------+| version() |+ ...
- 获取所有组合算法、获取全排列算法(java)
转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5574516.html 受到ACM1015的影响,个人感觉,有必要对统计学上的 全组合和全排列 进行一个简单的总结 ...
- yii 获取系统级请求参数的常用方法
1.GET/POST 1.1.获取GET/POST过来的数据 Yii::app()->request->getParam('id'); 1.2.判断数据提交方式 Yii::app()-&g ...
- 问题.NET访问 IIS 元数据库失败。
问题现象:访问 IIS 元数据库失败. 说明:执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: System.We ...
- makeKeyAndVisible的功能
makeKeyAndVisible的作用 [self.window makeKeyAndVisible] 由于iPhone是单窗口程序,所以也就只有这么一个Window对象,而且是UIWindow,不 ...
- 谁才是最快的消息队列:ActiveMQ, RabbitMQ[转]
Lately I performed a message queue benchmark, comparing several queuing frameworks (RabbitMQ, Active ...
- [Javascript] The Array forEach method
Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...
- 数据绑定(八)使用Binding的RelativeSource
当一个Binding有明白的数据来源时能够通过为Source或ElementName赋值的办法让Binding与之关联,有的时候因为不能确定Source的对象叫什么名字,但知道它与作为Binding目 ...
- [转] Spring - Java Based Configuration
PS: Spring boot注解,Configuration是生成一个config对象,@Bean指定对应的函数返回的是Bean对象,相当于XML定义,ConfigurationProperties ...
- Linux 释放cached内存
使用free -m 查看系统使用的内存情况: # free -m total used free shared buffers ...