UVA 11549 Calculator conundrum Floyd award ring

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions

Enter n K, which means the calculator can display n digits, initially with a number k

Each operation K = k^2, if the n bit is exceeded, the first n bits are intercepted.

The maximum number to be obtained.

Ideas:

First, we can tell that there must be a loop in this operation.

So how to terminate the loop, using the Floyd method

Let two loops child1 and child2 just start to be K, then child1 each time transformation, child2 each transformation 2 times;

This means that when child1 again equals child2, it is already at least a cyclic section, because Child2 has caught child1 from behind.


Import Java.io.printwriter;import java.util.arraylist;import Java.util.arrays;import Java.util.Collections;import Java.util.comparator;import Java.util.iterator;import Java.util.linkedlist;import Java.util.PriorityQueue;import Java.util.scanner;import Java.util.treeset;import Java.util.queue;public class Main {static int N = 100100;long N, K, MAXN Um;long work (long x) {x = X*x;while (x>=maxnum) x/= 10;return x;} void work () {int T = Cin.nextint (), while (t--> 0) {n = Cin.nextlong (); k = Cin.nextlong (); maxnum = 1;for (int i = 0; I &lt ; N i++) Maxnum *= 10;long ans = k;long a = k, b = k;do{a = Work (a); b = Work (b); ans = max (ans, b); b = Work (b); ans = max (ans, b);} while (a!=b); out.println (ANS);}} Main () {cin = new Scanner (system.in); out = new PrintWriter (System.out);} public static void Main (string[] args) {main E = new main (); E.work (); Out.close ();} Public Scanner cin;public static PrintWriter out;int upper_bound (int[] A, int l, int r, int val) {//upper_bound (a+l,a+r,val )-a;int pos = r;r--; WHILe (l <= R) {int mid = (l+r) >>1;if (a[mid]<=val) {L = mid+1;} else {pos = Mid;r = Mid-1;}} return POS;} /*class Queue {int[] queue = new Int[n+10];int front, rear;//front <= rearqueue () {//queue = new int[x];} void Clear () {front = rear = 1;} Boolean empty () {return front = = rear;} int size () {return rear-front;} int Front () {return queue[front];} int rear () {return queue[rear-1];} void push_rear (int x) {queue[rear++] = x;} void Pop_front () {front++;} void Pop_rear () {rear--;}} /**/int max (int x, int y) {return x > y x:y;} int min (int x, int y) {return x < y x:y;} Double Max (double x, double y) {return x > y x:y;} Double min (double x, double y) {return x < y x:y;} Long Max (long x, long y) {return x > y x:y;} Long min (long x, long y) {return x < y x:y;} static double EPS = 1e-8;int abs (int x) {return x > 0 ×:-X;} Double abs (double x) {return x > 0 ×:-X;} Boolean zero (double x) {return abs (x) < EPS;}}




UVA 11549 Calculator conundrum Floyd award ring

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.