1. 程式人生 > >[UVa 11549]Calculator Conundrum

[UVa 11549]Calculator Conundrum

math scan uva cto ... src 統計 size 無限

技術分享

題解

顯然按題意模擬會出現環,因為可能出現的數字數有限的,所以不可能無限的衍生下去。

那麽我們就可以按題意模擬,遍歷整個過程,統計最大值即可。

判環的環我們想到$hash$,也可以用$STL$中的$set$,但是復雜度高...

$Floyd$判圈。一步兩步法,有環的話肯定會相遇,空間復雜度可以降到$O(1)$,時間也快不少。

 1 //It is made by Awson on 2017.9.18
 2 #include <map>
 3 #include <set>
 4 #include <cmath>
 5 #include <ctime>
 6
#include <queue> 7 #include <stack> 8 #include <cstdio> 9 #include <string> 10 #include <vector> 11 #include <cstdlib> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 #define LL long long 16 #define Max(a, b) ((a) > (b) ? (a) : (b)) 17
#define Min(a, b) ((a) < (b) ? (a) : (b)) 18 #define Abs(a) ((a) < 0 ? (-(a)) : (a)) 19 using namespace std; 20 21 int n; 22 LL lim, k; 23 24 LL getnext(LL x) { 25 x *= x; 26 while (x/lim) x /= 10; 27 return x; 28 } 29 30 int main() { 31 int t; 32 scanf("%d", &t);
33 while (t--) { 34 scanf("%d%lld", &n, &k); 35 lim = 1; 36 LL ans = k; 37 for (int i = 1; i <= n; i++) 38 lim *= 10; 39 LL k1 = k, k2 = k; 40 do { 41 k1 = getnext(k1); ans = Max(ans, k1); 42 k2 = getnext(k2); ans = Max(ans, k2); 43 k2 = getnext(k2); ans = Max(ans, k2); 44 }while (k2 != k1); 45 printf("%lld\n", ans); 46 } 47 return 0; 48 }

[UVa 11549]Calculator Conundrum