HDU5908 Abelian Period(模拟暴力)

来源:互联网 发布:张辛苑的淘宝店 编辑:程序博客网 时间:2024/05/21 09:34

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


Abelian Period

 
 Accepts: 288
 
 Submissions: 984
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 262144/131072 K (Java/Others)
问题描述
SS是一个数字串,定义函数occ(S,x)occ(S,x)表示SS中数字xx的出现次数。例如:S=(1,2,2,1,3),occ(S,1)=2,occ(S,2)=2,occ(S,3)=1S=(1,2,2,1,3),occ(S,1)=2,occ(S,2)=2,occ(S,3)=1。如果对于任意的ii,都有occ(u,i)=occ(w,i)occ(u,i)=occ(w,i),那么我们认为数字串uuww匹配。例如:(1,2,2,1,3)\approx(1,3,2,1,2)(1,2,2,1,3)(1,3,2,1,2)。对于一个数字串SS和一个正整数kk,如果SS可以分成若干个长度为kk的连续子串,且这些子串两两匹配,那么我们称kk是串SS的一个完全阿贝尔周期。给定一个数字串SS,请找出它所有的完全阿贝尔周期。
输入描述
输入的第一行包含一个正整数T(1\leq T\leq10)T(1T10),表示测试数据的组数。对于每组数据,第一行包含一个正整数n(n\leq 100000)n(n100000),表示数字串的长度。第二行包含nn个正整数S_1,S_2,S_3,...,S_n(1\leq S_i\leq n)S1,S2,S3,...,Sn(1Sin),表示这个数字串。
输出描述
对于每组数据,输出一行若干个整数,从小到大输出所有合法的kk
输入样例
265 4 4 4 5 486 5 6 5 6 5 5 6
输出样例
3 62 4 8




解题思路:用有点技巧的暴力吧,有点像欧拉筛法筛素数的感觉,详情看代码吧。


/* ***********************************************┆  ┏┓   ┏┓ ┆┆┏┛┻━━━┛┻┓ ┆┆┃       ┃ ┆┆┃   ━   ┃ ┆┆┃ ┳┛ ┗┳ ┃ ┆┆┃       ┃ ┆┆┃   ┻   ┃ ┆┆┗━┓ 马 ┏━┛ ┆┆  ┃ 勒 ┃  ┆      ┆  ┃ 戈 ┗━━━┓ ┆┆  ┃ 壁     ┣┓┆┆  ┃ 的草泥马  ┏┛┆┆  ┗┓┓┏━┳┓┏┛ ┆┆   ┃┫┫ ┃┫┫ ┆┆   ┗┻┛ ┗┻┛ ┆************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <bitset>using namespace std;#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)#define pb push_back#define mp make_pairconst int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define LL long long#define ULL unsigned long long#define MS0(X) memset((X), 0, sizeof((X)))#define SelfType intSelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}#define Sd(X) int (X); scanf("%d", &X)#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())#define all(a) a.begin(), a.end()#define   mem(x,v)      memset(x,v,sizeof(x))typedef pair<int, int> pii;typedef pair<long long, long long> pll;typedef vector<int> vi;typedef vector<long long> vll;inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")const int N = 1e5 + 5;int a[N],t[N],p[N],q[N],ok[N];int cnt;int n;int check(int x){    cnt++;    for(int i=1;i<=x;i++)    {        t[a[i]] = cnt;        p[a[i]] = 0;    }    for(int i=1;i<=x;i++)        p[a[i]]++;    for(int i=1;i<n/x;i++)    {        int L = i * x + 1;        int R = L + x - 1;        for(int j=1;j<=x;j++) q[a[j]] = 0;        for(int j=L;j<=R;j++)        {            if(t[a[j]] != cnt) return 0;            if(++q[a[j]] > p[a[j]]) return 0;        }    }    return 1;}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);ios::sync_with_stdio(0);cin.tie(0);int t;t = read();while(t--)    {        n = read();        for(int i=1;i<=n;i++) a[i] = read();        MS0(ok);        for(int i=1;i<=n;i++)        {            if(n%i==0 && !ok[i])            {                if(check(i))                {                    for(int j=i;j<=n;j+=i)                    {                        if(n%j==0) ok[j] = 1;                    }                }            }        }        int flag = 0;        for(int i=1;i<=n;i++)        {            if(ok[i])            {                if(flag)printf(" ");                flag = 1;                printf("%d",i);            }        }        printf("\n");    }return 0;}


0 0
原创粉丝点击