HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5908

Description

Let S be a number string, and occ(S,x) means the times that number x occurs in S.

i.e. S=(1,2,2,1,3),occ(S,1)=2,occ(S,2)=2,occ(S,3)=1.

String u,w are matched if for each number i, occ(u,i)=occ(w,i) always holds.

i.e. (1,2,2,1,3)≈(1,3,2,1,2).

Let S be a string. An integer k is a full Abelian period of S if S can be partitioned into several continous substrings of length k, and all of these substrings are matched with each other.

Now given a string S, please find all of the numbers k that k is a full Abelian period of S.

Input

The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.

In each test case, the first line of the input contains an integer n(n≤100000), denoting the length of the string.

The second line of the input contains n integers S1,S2,S3,...,Sn(1≤Si≤n), denoting the elements of the string.

Output

For each test case, print a line with several integers, denoting all of the number k. You should print them in increasing order.

Sample Input

2

6

5 4 4 4 5 4

8

6 5 6 5 6 5 5 6

Sample Output

3 6

2 4 8

题意:

给你n个数字,首先你可以以没k个为一段,要求n分成的各个段中数字的数量和个数都相同。

题解:

首先枚举每个可以被n整除的段,然后再判断是否可以即可,枚举sqrt(n)或者n/2暴力都可以。

判断的方法:首先我们先统计每个元素的个数,对于k个元素一组,因为是均分的,所以每组出现多少个就确定了。现在我们从开始往后面扫,首先没k个一组,多少组就确定了,那么我们现在看,在每一段该元素都有每个数字的出现上限。如果超过了那么必然是错误的k划分,同时我们记录下到达该数字上限的数的个数,和之前的统计如果不同返回false。最终返回true,这样就求出。

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000+100;
int s[maxn+10];
int rec[maxn+10];
bool fuck[maxn+10];
int db[maxn+10];
int ans[maxn+10];
int n;
int tol = 0;
inline bool sol(int k)
{
int tt = n / k;
memset(db,0,sizeof db);
for (int i = 0; i < tt; i++){
int temp = 0;
for (int j = 1; j <= k; j++){
db[s[i*k+j]]++;
if (db[s[i*k+j]]*tt > rec[s[i*k+j]]*(i+1))
return false;
else if (db[s[i*k+j]]*tt == rec[s[i*k+j]]*(i+1))
temp++;
}
if (temp != tol)
return false;
}
return true;
}
int main()
{
int t;
scanf("%d",&t);
while (t--){
tol = 0;
int ta = 0;
memset(rec,0,sizeof rec) ;
scanf("%d",&n);
for (int i = 1; i <= n; i++){
scanf("%d",&s[i]);
rec[s[i]]++;
}
for (int i = 1; i <= maxn; i++)
if (rec[i] != 0)
tol++; for (int i = 1; i*2 <= n; i++){
if (n%i == 0){
if (sol(i))
printf("%d ",i);
}
}
printf("%d\n",n);
}
return 0;
}