Infinite Fraction Path
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1391 Accepted Submission(s): 267
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 ( [Math Processing Error] + 1)%N.
A path beginning from the i-th city would pass through the cities [Math Processing Error], 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[ [Math Processing Error]], D[ [Math Processing Error]], and so on.
The best infinite fraction path is the one with the largest relevant fraction
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 ( [Math Processing Error] + 1)%N.
A path beginning from the i-th city would pass through the cities [Math Processing Error], 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[ [Math Processing Error]], D[ [Math Processing Error]], 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.
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
Source
Recommend
jiangzijing2015
题意:给你一张有向图有n个点,i号点走向(i*i+1)%n,每个点有一个权值,权值为0~9,问从一个点走n-1次后,形成的字符串最大为多少
解题思路:暴力+剪枝即可,当前数字小的数字可以不继续进行,当前层走过的位置可以不继续进行
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n,ma,ans[150009],g;
int vis[150009];
char ch[150009];
int main()
{
int t,cas=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%s",&n,ch);
g=ma=0;
for(int i=0;i<n;i++) ma=max(ma,ch[i]-'0'),vis[i]=-1,ans[i]=-1;
queue<int>q[2];
for(int i=0;i<n;i++)
if(ma==ch[i]-'0') q[g&1].push(i),vis[i]=g;
ans[0]=ma;
while(g<n)
{
int x=g+1;
queue<int>temp;
while(!q[g&1].empty())
{
int pre=q[g&1].front();
q[g&1].pop();
temp.push(pre);
ans[x]=max(ans[x],ch[(1LL*pre*pre+1LL)%n]-'0');
}
g++;
while(!temp.empty())
{
int pre=temp.front();
temp.pop();
int nt=(1LL*pre*pre+1LL)%n;
if(ch[nt]-'0'==ans[x]&&vis[nt]<g) q[g&1].push(nt), vis[nt]=g;
}
}
printf("Case #%d: ",++cas);
for(int i=0;i<n;i++) printf("%d",ans[i]);
printf("\n");
}
return 0;
}