zoukankan      html  css  js  c++  java
  • uva11549 Floyd判圈法

    题意:

    给两个数n, k,每次将k平方取k的前n位,问所有出现过的数的最大值

    原来这就是floyd判圈法。。

     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<cstring>
     6 #include<string>
     7 
     8 template<typename Q> Q read(Q& x) {
     9     static char c;
    10     static bool f;
    11     for(f = 0; c = getchar(), !isdigit(c); ) if(c == '-') f = 1;
    12     for(x = 0; isdigit(c); c = getchar()) x = x * 10 + c - '0';
    13     if(f) x = -x; return x;
    14 }
    15 template<typename Q> Q read() {
    16     static Q x; read(x); return x;
    17 }
    18 
    19 typedef long long LL;
    20 
    21 int next(int n, int x) {
    22     LL x2 = (LL) x * x;
    23     int len = 0;
    24     for(LL t = x2; t; t /= 10) len++;
    25     if(len > n) for(int t = len - n; t--; x2 /= 10);
    26     return x2;
    27 }
    28 
    29 int main() {
    30 #ifdef DEBUG
    31     freopen("in.txt", "r", stdin);
    32     freopen("out.txt", "w", stdout);
    33 #endif
    34     
    35     int T = read<int>();
    36     while(T--) {
    37         int n = read<int>(), k = read<int>();
    38         int ans = k, k1 = k, k2 = k;
    39         do {
    40             k1 = next(n, k1);
    41             k2 = next(n, k2); ans = std::max(ans, k2);
    42             k2 = next(n, k2); ans = std::max(ans, k2);
    43         }while(k1 != k2);
    44         printf("%d
    ", ans);
    45     }
    46     
    47     return 0;
    48 }
  • 相关阅读:
    TCMalloc 内存分配原理简析
    技术人沟通中的几个常见问题
    不同路径
    Js将字符串转数字的方式
    防抖节流模式
    Location对象
    React生命周期
    fgrep命令
    数据访问对象模式
    保持城市天际线
  • 原文地址:https://www.cnblogs.com/showson/p/5011956.html
Copyright © 2011-2022 走看看