• UVA11549 Calculator Conundrum


    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

    不难发现答案会出现循环,相当于在一个有限状态自动机上求环,使用floyd判圈法即可

    如何证明一定能走完整个环,以及复杂度如何?

    设环长为L,走了x步,则

    x mod L = 2x mod L

    即2x = x mod L

    也就是x = 0 mod L

    x最小为L

    也就是一定在L步相遇,且L步前不相遇

    这样走一步的那个人一定能走过整个环

    复杂度O(L)

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <algorithm>
     6 #include <queue>
     7 #include <vector>
     8 #define min(a, b) ((a) < (b) ? (a) : (b))
     9 #define max(a, b) ((a) > (b) ? (a) : (b))
    10 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
    11 inline void swap(long long &a, long long &b)
    12 {
    13     long long tmp = a;a = b;b = tmp;
    14 }
    15 inline void read(long long &x)
    16 {
    17     x = 0;char ch = getchar(), c = ch;
    18     while(ch < '0' || ch > '9') c = ch, ch = getchar();
    19     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
    20     if(c == '-') x = -x;
    21 }
    22 
    23 const long long INF = 0x3f3f3f3f;
    24 
    25 long long t, ma, ans;
    26 
    27 long long run(long long a)
    28 {
    29     a *= a;
    30     while(a >= ma) 
    31         a /= 10;
    32     return a;
    33 }
    34 
    35 int main()
    36 {
    37     read(t);
    38     for(;t;--t)
    39     {
    40         long long k,n;read(k), read(n);
    41         ma = 1;
    42         for(register long long i = 1;i <= k;++ i)  ma *= 10;
    43         long long k1 = n, k2 = n;
    44         ans = n;
    45         do
    46         {
    47             k1 = run(k1), k2 = run(k2), ans = max(ans, k1);
    48             k1 = run(k1), ans = max(ans, k1);
    49         }while(k1 != k2);
    50         printf("%lld
    ", ans);
    51     }
    52     return 0;
    53 } 
    UVA11549
  • 相关阅读:
    [组合数取模] 方法汇总
    机房收费系统(VB.NET)——存储过程实战
    Remove Duplicates from Sorted Array
    Android ListFragment实例Demo(自己定义适配器)
    混合模式程序集是针对“v1.1.4322”版的执行时生成的,在没有配置其它信息的情况下,无法在 4.0 执行时中载入该程序集。
    小编接地气——第六届中国云计算大会攻略Q&amp;A
    有统计的百度分享
    Yii CGridView 基本使用(三)关联表相关字段搜索
    扯谈网络编程之Tcp SYN flood洪水攻击
    多平台响应键盘事件!(适用于Cocos2dx 3.0 alpha以上版本号)
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/8287259.html
Copyright © 2020-2023  润新知