The Ant welly now dedicates himself to urban infrastructure. He came to the Kingdom of numbers and solicited a audience with the king. He recounted how he had built a happy path in the Kingdom of happiness. The king affirmed Welly ' s talent and hoped that's talent can help him find the best infinite fraction path before the A Nniversary.
The kingdom have N cities numbered from 0 to N-1 and you is given an array d[0 ... N-1] of decimal digits (0≤d[i]≤9, d[i] is an integer). The destination of the one-way road start from the I-th city are the city labelled (I2 I2 + 1)%N.
A path beginning from the i-th city would pass through the citiesu1,u2,u3 U1,U2,U3, and so on consecutively. The path constructs a real number a[i], called the relevant fraction such that the integer part of it are equal to zero and Its fractional part is a infinite decimal fraction with digits d[i], d[u1 u1], D[ u 2 "> u 2 u2], and so on.
the best infinite Fraction path is the one with the largest relevant fraction
Inputthe input contains multiple test cases and the first line provides a integer up to + indicating to the total Numbe ROF test Cases.
For each test case, the first line contains the integer N (1≤n≤150000). The second line contains an array ofdigits D, given without spaces.
The summation of N is smaller than 2000000.
Outputfor Each test case, you should output the label of the case first. Then your is to output exactly n characters which is the first N digits of the fractional part of the largest relevant FR Action.
Sample Input
43149512345732145679261025520
Sample Output
Case #1:999Case #2:53123Case #3:7166666Case #4:615015015
Exercises
If you want to have a larger number, the higher the number, the better, the next high, and the second highest. Then we can first find the points of the most important points in the point as the starting point into the queue, then step by step to do a breadth-first search, in turn, exclude the starting point of the smaller number. The last remaining point is the answer.
?? But this definitely time out, the complexity of the largest O (n2) O (N2), think about it will find that the figure has many features: the graph is composed of many unidirectional chains and rings, some links to the middle of the other chains, the end of all chains must be connected to a ring. The reason for the timeout must be that many points have been repeatedly searched. There may be a point that has been searched by multiple starting points (the sub-expenditure of the chain); a point is searched (loop) by a starting point.
?? This is then optimized in this direction:
?? 1. In the same layer (step), we only have the starting point corresponding to the point at which the current weights are greatest.
?? 2. For the branch of the chain: if the initial point AA searches for a point that has been searched by the dot Bb, then the initial point BB will not have to continue to search (think about why), BB can be moved out of the queue.
Reference code:
#include <bits/stdc++.h> #define CLR (A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;const int inf=0x3f3f3f3f;const int maxn=250010;int Max,vis[maxn],tot;char a[maxn],ans[maxn];ll n;struct Node{int step; ll POS; Node () {} node (int step,ll pos): Step (Step), POS (POS) {}};queue<node >q;inline void BFs () {Node s; while (!q.empty ()) {S=q.front (); Q.pop (); if (s.step==n) continue; if (A[s.pos]==ans[s.step]) {if (vis[s.pos]==s.step) continue; Vis[s.pos]=s.step; s.pos= (S.pos * s.pos + 1)% n; s.step++; if (A[s.pos]>=ans[s.step]) {Ans[s.step]=a[s.pos]; Q.push (s); }}}}int Main () {int T; cin>>t; for (int. Cas=1;cas<=t;++cas) {while (!q.empty ()) Q.pop (); tot=0; scanf ("%lld", &n); max=0; scanf ("%s", a); for (int i=0; i<n; i++) Max=max (Max, (iNT) a[i]); for (int i=0; i<n; i++) if (A[i]==max) Q.push (Node (1,i)); CLR (ans,-1); CLR (vis,-1); Ans[1]=max; BFS (); printf ("Case #%d:", CAs); ans[n+1]= ' + '; printf ("%s\n", ans+1); } return 0;}
ACM/ICPC Shenyang G-Question Infinite fraction Path