Algorithm title: UVa 11549

Source: Internet
Author: User

Topic link

Direct simulation of the calculation process. You can see that the calculator shows the number is circular, the key lies in the simulation process, how to determine whether the cycle.

You can use the map or set in STL, but the efficiency is low. Hash words consume a lot of space.

You can tell from the big white that there is also a kind of thing called "Floyd Circle Method". It's fake. There are two children running on a circle track and starting at the same time, but one child is twice times faster than the other, so the fast-running child will "catch up" with the slow-running child (more than n laps). When more than that moment, to be sure, this time the fast child has at least 1 laps, that is, "after a cycle." So as long as the simulation track record fast.

#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <set>  
using namespace std;  
    
typedef long long Int64;  
    
Char buf[12];  
Int64 GetNext (int n, int64 k) {  
    Int64 now = (Int64) k*k;  
    int len = 0;  
    Int64 tmp = now;  
    while (tmp>0) {++len. tmp/=}  
    while (Len-n > 0) {--len. Now/=}  
    return to now;  
}  
    
    
int main () {  
    int ncase, n;  
    Int64 K;  
    scanf ("%d", &ncase);  
    while (ncase--) {  
        
        cin >> n >> K;  
        Int64 ans = k;  
        Int64 slow=k, fast=k;  
        do{  
            slow = GetNext (n, slow);  
            Fast = GetNext (n, fast);  ans = max (ans, fast);  
            Fast = GetNext (n, fast);  Ans  = max (ans, fast); while  
        (slow!= fast);  
        cout << ans << endl;  
    return 0;  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.