思路:
除非n无限大,否则都会出现循环。
既然会循环,也就是说会有“环”。
此时,便可以用floyd判环来解决这个问题,复杂度为O(n)。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vll;
const int MAXN = 1e6 + 10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const double eps = 1e-8;
int n, m;
ll k;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int times;
cin >> times;
while(times--){
cin >> n >> k;
ll res = k, top = 0;
for(int i = 0; i < n; i++)
top = top * 10 + 9;
ll u, v;
u = v = k;
while(true){
u = u * u;
while(u > top)
u /= 10;
res = max(res, u);
v = v * v;
while(v > top)
v /= 10;
res = max(res, v);
v = v * v;
while(v > top)
v /= 10;
res = max(res, v);
if(u == v)
break;
}
cout << res << endl;
}
cerr << "execute time : " << (double)clock() / CLOCKS_PER_SEC << endl;
return 0;
}
未来可期。