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. A better way to learn D3 js - iLearning D3.js

    iLearning D3.js Basic is an iPad app to learn and code with D3. In 1.1 version, new tutorial is prov ...

  2. Spring笔记(三)AOP前篇之动态代理

    AOP思想是将程序中的业务代码与服务代码进行分离,在运行时进行结合.比较强调程序的层次结构,是一种面向切面的编程.而在AOP实现的底层主要用到了动态代理,而动态代理又分为JDK动态代理和CGLIB动态 ...

  3. hdoj 2 括号配对问题【数组模拟实现+STL实现】

    栈遵循先进后出的原则 括号配对问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在,有一行括号序列,请你检查这行括号是否配对.   输入 第一行输入一个数N(0 ...

  4. hdoj 2553 N皇后问题【回溯+打表】

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. 【转】Spring 4.x实现Restful web service

    http://my.oschina.net/yuyidi/blog/352909 首先我们还是跟之前一样,创建一个maven项目,不过因为Spring Restful web service是基于Sp ...

  6. Count属性(行数 @)

    在PS2.0中,如果返回值为空,则count也会返回空(vm.txt内容为空),如下: (gc d:\vm.txt).count 如果加上@的话,会返回0 @(gc d:\vm.txt).count ...

  7. Xenomai 的模式切换浅析

    在Xenomai的用户空间下,有两种模式:primary mode (主模式) 和 secondary mode(次模式). 在主模式下调用Linux系统调用后程序就会进入次模式,反之,在次模式下调用 ...

  8. java发送带附件的邮件

    /** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...

  9. JSON数据的中文乱码问题

    问题描述: 在firefox中返回的JSON数据没有出现乱码,但在GridPanel中显示乱码,而jsp页面和xml文件都是utf-8类型. 解决方案: 在 PrintWriter out = res ...

  10. GOOGLE------Reilly_Open_Source_Award

    https://en.wikipedia.org/wiki/O%27Reilly_Open_Source_Award#2016