就觉得这题的暴力解法时间复杂度应该会很高,可能循环节会比较小吧
比较好的收获就是这个Floyd判环法
#include <set> #include <cstdio> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 100005; const int INF = 0x3f3f3f3f; LL pow10 (int n) { LL ans = 1; for(int i = 1;i<=n;++i)ans*=10; return ans; } LL m; LL next(LL k) { k*=k; while(k>=m)k/=10; return k; } int main() { // freopen("in.txt","r",stdin); int T;scanf("%d",&T); while(T--) { LL n,k; cin>>n>>k; m = pow10(n); LL k1 = k,k2 = k,ans = k; do { k1 = next(k1); k2 = next(k2);ans=max(ans,k2); k2 = next(k2);ans=max(ans,k2); } while(k1!=k2); cout<<ans<<endl; } return 0; }
时间: 2024-10-29 22:10:09