当前位置: 代码迷 >> 综合 >> UVA 11549 Calculator Conundrum Floyd判圈算法 .
  详细解决方案

UVA 11549 Calculator Conundrum Floyd判圈算法 .

热度:103   发布时间:2023-09-23 05:37:48.0

题目地址:http://vjudge.net/problem/UVA-11549

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<=(b);++i)
#define REPD(i,a,b) for(int i=a;i>=(b);--i)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int buf[20];
int next(int n,int x){if(!x) return 0;int p=0;long long k=(long long) x*x;while(k){buf[p++]=k%10;k/=10;}if(p<n) n=p;int ans=0;REP(i,1,n) {ans=ans*10+buf[--p];}return ans;
}
int main(int argc, char const *argv[])
{int T; scanf("%d",&T);while(T--){int n,k;scanf("%d%d",&n,&k);int ans=k,k1=k,k2=k;do{k1=next(n,k1);k2=next(n,k2); ans=max(ans,k2);k2=next(n,k2); ans=max(ans,k2);}while(k2!=k1);printf("%d\n", ans);}return 0;
}