UVA11549 Calculator Conundrum循环节

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11549 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?”
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.
Output
For each test case, print the maximum number that Alice can get by repeatedly squaring the starting number as described.
Sample Input
2
1 6
2 99
Sample Output
9
99

问题链接UVA11549 Calculator Conundrum
问题简述:(略)
问题分析:循环节问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11549 Calculator Conundrum */

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ULL;

int main()
{
    int t, n;
    scanf("%d", &t);
    while (t--) {
        ULL k;

        scanf("%d%llu", &n, &k);

        set<ULL> s;
        ULL all9 = 9, cur = k, ans = 0;
        for (int i = 2; i <= n; i++)
            all9 = all9 * 10 + 9;
        for (; ;) {
            if (s.count(cur)) break;
            s.insert(cur);
            ans = max(ans, cur);
            cur *= cur;
            while (cur > all9) cur /= 10;
        }

        printf("%llu\\n", ans);
    }

    return 0;
}

以上是关于UVA11549 Calculator Conundrum循环节的主要内容,如果未能解决你的问题,请参考以下文章

uva 11549 Calculator Conundrum

UVa11549:Calculator Conundrum

[UVa 11549]Calculator Conundrum

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

UVA11549 计算机谜题(Floyd判圈算法)

Floyd判圈算法