hdu 6223

Infinite Fraction Path

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1923 Accepted Submission(s): 388

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
题意:给出一个数组,第i位的下一位是(i*i+1)%n;问以第i为位开始后面n位字典序最大的那个字符串。
做法1:直接暴力比较前20位,复杂度20*2000000,因为nex[i]=(i*i+1)%n;很容易出现环,如果前50位不相同后面不相同的几率很小。
做法2:改后缀数组的板子,找出最大的那一位,复杂度n*logn;
做法3:建立st表,nex[i][j]代表第i位后面第(1 < < j)位的下标,st[i][j]代表以第i位为起始,长度为1 < < j位的hash值,比较任意两个下标开始的字符串只需要log次,复杂度nlogn。
代码只写了做法1和做法3,后缀数组还不算是特别懂。

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+100;
char st[N];
int T,n;
bool judge(int x,int y){
    for(int i = 1;i <= 20;i ++){
        if(st[x] != st[y]){
            return st[x] < st[y];
        }
        x = (1LL*x*x+1)%n;
        y = (1LL*y*y+1)%n;
    }
}
void print(int x){
    for(int i = 1;i <= n;i ++){
        printf("%c",st[x]);
        x = (1LL*x*x+1)%n;
    }
    puts("");
}
int main(){
    cin >> T;
    int kcase = 1;
    while(T--){
        scanf("%d",&n);
        scanf("%s",st);
        int mx = 0;
        for(int i = 1;i < n;i ++){
            if(judge(mx,i)) mx = i;
        }
        printf("Case #%d: ",kcase++);
        print(mx);
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+100;
const int mod = 1e9+7;
int st[20][N];
int nex[20][N];
int qp[1000007];
int T,n,kcase=1;
char s[N];
void init(){
    qp[0] = 1;
    for(int i = 1;i < 1000007;i ++) qp[i] = 1LL*qp[i-1]*10%mod;
}
void st_init(){
    for(int i = 0;i < n;i ++) nex[0][i] = (1ll*i*i+1)%n;
    for(int i = 1;i < 20;i ++){
        for(int j = 0;j < n;j ++)
            nex[i][j] = nex[i-1][nex[i-1][j]];
    }
    for(int i = 0;i < n;i ++) st[0][i] = s[i]-'0';
    for(int i =1;i < 20;i ++){
        for(int j = 0;j < n;j ++){
            st[i][j] = (1ll*st[i-1][j]*qp[1<<(i-1)]+st[i-1][nex[i-1][j]])%mod;
        }
    }
}
bool judge(int x,int y,int b){

    if(b == 0) return s[x] > s[y];
    //cout << x << ' ' << y << ' ' << b << ' '<< st[b-1][x] << ' '<<  st[b-1][y] << endl;
    if(st[b-1][x] != st[b-1][y])
        return judge(x,y,b-1);
    else return judge(nex[b-1][x],nex[b-1][y],b-1);
}
void print(int x){
    for(int i = 1;i <= n;i ++){
        printf("%c",s[x]);
        x = (1LL*x*x+1)%n;
    }
    puts("");
}
int main(){
    cin >> T;
    init();
    while(T--){
        scanf("%d",&n);
        scanf("%s",s);
        st_init();

        int mx = 0;
        for(int i = 1;i < n;i ++){
            if(judge(i,mx,18)){
                mx = i;
            }
        }
        printf("Case #%d: ",kcase++);
        print(mx);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值