Chapter 1 of algorithm competition-Training Guide-1.19 _ ultraviolet A 11549

Source: Internet
Author: User

This is a simple question, mainly because the hash range is large and there is no way to solve it, but it can also be solved by using map or something, but I am not thinking about it.

For simple hash big data, if the key is of the int type, use a set table directly.

Then the rest of the processing is actually very simple. However, this is because you do not have to think hard.

A simple introduction to this question: an old computer, the screen can only display n numbers, now give you a number, each time the square, overflow as long as the maximum first n digits, continue to square and ask you, what is the maximum N-digit number you have obtained.

It is very simple, but there may be some problems in hash, but it is a small problem. Then there is the selection of methods. The question solution gives a Floyd method ring, which is explained in this way. There are two children running in competitions. One child is faster than the other. If there is a ring, the child will be able to catch up with the child who is slow, and the two will meet each other.

But in fact, how fast should we make quick children? Is it faster? The answer is definitely not. In fact, I have verified programming, and this is not the case. Of course, if you give a very large ring and a very large ring, it is indeed very fast and really good, but for the computer, you are big enough to be able to see fast, and fast. It's hard to meet each other. Sometimes you can run around without meeting each other, isn't it finished? (The rule for meeting is that a child just runs at a complete speed of 3 Mb/s, so you have to talk about the meeting after at least 3 Mb/s)

This idea is good. Fortunately, I have never touched it before, and now I have come into contact with it.

Paste the Code:

#include <stdio.h>#include <string.h>#include <iostream>#include <string>using namespace std;int N, K;int next(int k){long long ans = (long long)k * k;int buf[22];int cnt = 0;while (ans){buf[cnt++] = ans % 10;ans /= 10;}int L = cnt - 1;if (cnt > N){cnt = N;}int sum = 0;for (int i = 0; i < cnt; i++){sum = sum * 10 + buf[L--];}return sum;}int main(){int T;scanf("%d", &T);while (T--){scanf("%d%d", &N, &K);int k1 = K;int k2 = K;int ans = K;do{k1 = next(k1);k2 = next(k2);if (ans < k2){ans = k2;}k2 = next(k2);if (ans < k2){ans = k2;}} while (k1 != k2);printf("%d\n", ans);}//system("pause");return 0;}

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: 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.