HDU-6223

题意

长度为 $n(1\le n \le 2e6)$ 的数组 $(0\le a_i\le 9)$, 下标为 $[0,n-1]$ , 你要取 $n$ 个数构成一个长度为 $n$ 的数组 $b$ , 假设第一次选 $a[i]$ , 则 $b[1]=a[i], b[2]=a[(i^2+1)\%n],\cdots$

输出字典序最小的 $b$

题解

显然取的下标序列存在循环节, 由打表可知, 循环节的长度不会超过 $21$, 所以我们只用枚举前 $21*20$ (循环节的最大$lcm$)位就可以找到保证 $b$ 字典序最小的起始点, 好像数据太水了,只用枚举前 21​ 位就可以了

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
using namespace std;
#define forl(i, l, r) for (int i = l; i <= r; i++)
#define forr(i, r, l) for (int i = r; i >= l; i--)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define fro1(i, n) for (int i = 1; i <= n; i++)
#define for0(i, n) for (int i = 0; i < n; i++)
#define fro0(i, n) for (int i = 0; i < n; i++)
#define meminf(a) memset(a, inf, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define memcp(a,b) memcpy(a,b,sizeof(b))
#define oper(type) bool operator <(const type y)const
#define mp make_pair
#define pu_b push_back
#define pu_f push_front
#define po_b pop_back
#define po_f pop_front
#define fi first
#define se second
#define whiel while
#define retrun return
typedef pair<long long, long long> pll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vii;
typedef long double db;
typedef long long ll;
typedef int itn;
int in(int &a,int &b,int &c,int &d){return scanf("%d%d%d%d",&a,&b,&c,&d);}
int in(int &a,int &b,int &c){return scanf("%d%d%d",&a,&b,&c);}
int in(int &a,int &b){return scanf("%d%d",&a,&b);}
int in(ll &a){return scanf("%lld",&a);}
int in(int &a){return scanf("%d",&a);}
int in(char *s){return scanf("%s",s);}
int in(char &s){return scanf("%c",&s);}
int in(db &a){return scanf("%Lf",&a);}
void out(int a){printf("%d ",a);}
void outln(int a){printf("%d\n",a);}
void out(ll a){printf("%lld ",a);}
void outln(ll a){printf("%lld\n",a);}
const db pi = acos((db)-1);
const ll inf =0x3f3f3f3f;
const db eps = 1e-8;
const int N = 1.6e5;
const ll mod = 1e9+7;
int sign(db a) { return a < -eps ? -1 : a > eps;}
int db_cmp(db a, db b){ return sign(a-b); }

char s[N],ans[N];
int main() {
int t,n;
in(t);
for1(ca,t){
in(n);
in(s);
printf("Case #%d: ",ca);
ll loo;
for0(i,n)ans[i]='0';
for0(i,n){
bool yes=0;
ll ti=i;
for0(j,22){
if(s[ti]<ans[j]){
if(yes)ans[j]=s[ti];
else break;
}else if(s[ti]>ans[j]){
yes=1;
ans[j]=s[ti];
}
ti=(ti*ti+1)%n;
}
if(yes)loo=i;
}
for0(i,n){
putchar(s[loo]);
loo=(loo*loo+1)%n;
}
puts("");
}
return 0;
}