Infinite Fraction Path
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2822 Accepted Submission(s): 566
Problem Description
The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and solicited an 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 this talent can help him find the best infinite fraction path before the anniversary.
The kingdom has N cities numbered from 0 to N - 1 and you are given an array D[0 ... N - 1] of decimal digits (0 ≤ D[i] ≤ 9, D[i] is an integer). The destination of the only one-way road start from the i-th city is the city labelled (i2 + 1)%N.
A path beginning from the i-th city would pass through the cities 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 is equal to zero and its fractional part is an infinite decimal fraction with digits D[i], D[u1], D[u2], and so on.
The best infinite fraction path is the one with the largest relevant fraction
Input
The input contains multiple test cases and the first line provides an integer up to 100 indicating to the total numberof 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.
Output
For each test case, you should output the label of the case first. Then you are to output exactly N characters which are the first N digits of the fractional part of the largest relevant fraction.
Sample Input
4 3 149 5 12345 7 3214567 9 261025520
Sample Output
Case #1: 999 Case #2: 53123 Case #3: 7166666 Case #4: 615015015
题意:
给你长度为n的字符串,你选中一个起点x(x∈[0,n)),然后开始跳n-1次,假设当前点下标为i,下次就会跳到标号为(i*i+1)%n的点上,最后你可以得到一个新的长度为n的字符串,求出最大的字典序情况
思路:
这题有个很强的结论,只要知道了就是道水题
考虑暴力每个x,然后O(n)判定形成的字符串字典序是否比当前的最优解要大,复杂度O(n²)
而这个结论就是:没有必要每次O(n),只要前500个字符一样,那么后面的一定都一样!所以>500直接break,复杂度O(500n), 可以过!
证明:对于所有的下标k,(k*k+1)%n向k连一条有向边,最后可以得到若干棵基环树构成的森林,这个森林有三个性质:①基环树特别的多;②每棵基环树环特别的小;③叶子巨多;这样子的话,字符串一定会很快进入一个环,两个字符串前面相等后面就一定都相等了
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
char str[150005];
int n, p, last[150005], t[150005], vis[150005];
int main(void)
{
int T, i, bet, c1, c2, q, cas = 1;
scanf("%d", &T);
while(T--)
{
bet = p = 0;
scanf("%d%s", &n, str);
for(i=0;i<=n-1;i++)
last[i] = 150002;
for(i=0;i<=n-1;i++)
{
vis[i] = 0;
bet = max(bet, (int)str[i]);
last[((LL)i*i+1)%n] = i;
t[i] = ((LL)i*i+1)%n;
}
while(vis[p]==0)
{
vis[p] = 1;
p = t[p];
}
printf("Case #%d: ", cas++);
for(i=0;i<=n-1;i++)
{
if(str[i]==bet && str[last[i]]!=bet)
{
c1 = p, c2 = i;
for(q=1;q<=500;q++)
{
if(str[c1]>str[c2])
break;
if(str[c1]<str[c2])
{
p = i;
break;
}
c1 = t[c1];
c2 = t[c2];
}
}
}
for(i=1;i<=n;i++)
{
printf("%c", str[p]);
p = t[p];
}
puts("");
}
return 0;
}