码迷,mamicode.com
首页 > 其他好文 > 详细

UVA11549 Calculator Conundrum 计算器谜题

时间:2015-01-22 12:44:49      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

  就觉得这题的暴力解法时间复杂度应该会很高,可能循环节会比较小吧

  比较好的收获就是这个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;
}

 

UVA11549 Calculator Conundrum 计算器谜题

标签:

原文地址:http://www.cnblogs.com/GJKACAC/p/4241042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!