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 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 (1 ≤ ≤ 200), the number of test cases. Each test case contains two integers (1 ≤ ≤ 9) and (0 ≤ < 10n) where n is the number of digits this calculator can display 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判圈算法)的更多相关文章

  1. UVA 11549 Calculator Conundrum (Floyd判圈算法)

    题意:有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 思路:这个题一定会出现 ...

  2. Floyd判圈算法 UVA 11549 - Calculator Conundrum

    题意:给定一个数k,每次计算k的平方,然后截取最高的n位,然后不断重复这两个步骤,问这样可以得到的最大的数是多少? Floyd判圈算法:这个算法用在循环问题中,例如这个题目中,在不断重复中,一定有一个 ...

  3. UVa 11549 计算器谜题(Floyd判圈算法)

    https://vjudge.net/problem/UVA-11549 题意: 有一个老式计算器,只能显示n位数字,输入一个整数k,然后反复平方,如果溢出的话,计算器会显示结果的最高n位.如果一直这 ...

  4. 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 ...

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

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

  6. Floyd判圈算法

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

  7. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  8. Floyd 判圈算法

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

  9. Floyd判圈算法 Floyd Cycle Detection Algorithm

    2018-01-13 20:55:56 Floyd判圈算法(Floyd Cycle Detection Algorithm),又称龟兔赛跑算法(Tortoise and Hare Algorithm) ...

随机推荐

  1. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程06:技能播放的逻辑关系》

    6.技能播放的逻辑关系 技能播放概述: 当完成对技能输入与检测之后,程序就该对输入在缓存器中的按键操作与程序读取的技能表信息进行匹配,根据匹配结果播放相应的连招技能. 技能播放原理: 按键缓存器中内容 ...

  2. Linux 下svn恢复到某一版本

    经常由于坑爹的需求,功能要切回到之前的某一个版本.有两种方法可以实现: 方法1: 用svn merge 1) 先 svn up,保证更新到最新的版本,如20: 2) 然后用 svn log ,查看历史 ...

  3. HW2.23

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  4. 问题-delphi 调试(F8)错行处理

    在delphi 7中(其他版本也可能碰到该问题),编译后,代码左边显示蓝色小点,表示哪句代 码是被编译了的,可今天我的一些代码不能显示那些蓝点了,这样也就不能在上面设断点了,而且用F8跟踪发现执行的顺 ...

  5. 关于easyui模拟win2012桌面的一个例子系列

    最近时间比较充裕,想到之前领导问我,什么界面更适合公司这种屏幕小但是又要求可以同时处理更多的工作. 我感觉  windows是最合适的,毕竟微软已经做了这么多年的系统了,人的操作习惯已经被他们确定了. ...

  6. 七行jquery代码实现图片渐变切换【兼容ie6+、 Chrome、 Firefox】

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. git 秘钥的生成

    在命令查看自己的秘钥还是公钥 cat .ssh/id_rsa.pub/cat .ssh/id_rsa

  8. GridControl 选择列、复选框全选(上)

    说明: GirdControl 中添加一列,这一列不是写在数据库中的,而是代码中添加的. 图示: 底层类代码: #region GridControl 全选 /// <summary> / ...

  9. PHP class_exists 检查类是否已定义

    (PHP 4, PHP 5)  class_exists — 检查类是否已定义 bool class_exists ( string $class_name [, bool $autoload ] ) ...

  10. 2假动作,数据缓冲,CCEaseExponential,CCEaseElastic,CCEaseBounce,CCCallFunc,funcNCallBack,funcNDCallBack,funcO

     1 缓冲动作 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/4 ...