Abelian Period

题目连接:

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

Hint

题意

设S是一个数字串,定义函数occ(S,x)表示S中数字x的出现次数。

例如:S=(1,2,2,1,3),occ(S,1)=2,occ(S,2)=2,occ(S,3)=1。

如果对于任意的i,都有occ(u,i)=occ(w,i),那么我们认为数字串u和w匹配。

例如:(1,2,2,1,3)≈(1,3,2,1,2)

对于一个数字串S和一个正整数k,如果S可以分成若干个长度kk的连续子串,且这些子串两两匹配,那么我们称k是串S的一个完全阿贝尔周期。

给定一个数字串S,请找出它所有的完全阿贝尔周期。

题解:

枚举k,首先k必须是n的约数,然后就能算出每个数字应该出现多少次,O(n)检验即可。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int ok[maxn],a[maxn],c1[maxn],c2[maxn],n;
bool check(int x)
{memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2));for(int i=1;i<=x;i++)c1[a[i]]++;for(int i=2;i<=n/x;i++){int l=(i-1)*(x)+1;int r=i*(x);for(int j=l;j<=r;j++){c2[a[j]]++;if(c2[a[j]]>c1[a[j]])return 0;}for(int j=l;j<=r;j++)c2[a[j]]--;}return 1;
}
void solve()
{scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);memset(ok,0,sizeof(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 fi=0;for(int i=1;i<=n;i++){if(ok[i]){if(fi==0)printf("%d",i),fi=1;else printf(" %d",i);}}printf("\n");
}
int main()
{int t;scanf("%d",&t);while(t--)solve();return 0;
}

转载于:https://www.cnblogs.com/qscqesze/p/5927590.html

HDU 5908 Abelian Period 暴力相关推荐

  1. hdu 4587 TWO NODES 暴力枚举+tarjan

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587 题意是拿掉两个点 求最多可以把整个图分成几个联通块 注意到有一个模板是可以通过找割点来快速求出 ...

  2. HDU 4831 Scenic Popularity 暴力模拟

    Scenic Popularity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. hdu 4775 Infinite Go(暴力)

    题目链接:hdu 4775 Infinite Go 题目大意:两个人下围棋,总共走了n步.黑棋和白棋交替走,假设一片棋的上下左右被封死,那么该片棋子就会被吃掉,问说最后黑白棋各剩多少个. 解题思路:比 ...

  4. The Little Girl who Picks Mushrooms HDU - 4422 (枚举暴力)

    题目描述:5个包,已经有n个包里装了东西,还要继续往剩下的包里装东西(想装多少装多少).返程要经过两个过程,一是要交出3个东西数量之和恰好为1024整数倍的包:二是剩下两个包的东西每次都会被偷1024 ...

  5. hdu 4956 Poor Hanamichi(暴力)

    题目链接:hdu 4956 Poor Hanamichi 题目大意:Hanamichi要解决一个问题,即对给定区间l,r,求有多少个数的奇数位和比偶数位和大3,然后Hanamichi推出一个公式(r ...

  6. HDU - 6635 Nonsense Time (暴力LIS)

    题目链接 题意 A,BA,BA,B两个数组,B[i]表示A中下表为B[i]B[i]B[i]的数字当前可用,求B[i]B[i]B[i]对应的AAA中的LISLISLIS,其中AAA中是[1,n][1,n ...

  7. 数学--数论--HDU 2582 F(N) 暴力打表找规律

    This time I need you to calculate the f(n) . (3<=n<=1000000) f(n)= Gcd(3)+Gcd(4)+-+Gcd(i)+-+Gc ...

  8. html2image乱码问题,HtmlImageGenerator字体乱码问题解决、html2image放linux上乱码问题解决...

    使用html2image-0.9.jar生成图片. 在本地window系统正常,放到服务器linux系统时候中文乱码问题.英文可以,中文乱码应该就是字体问题了. 一.首先需要在linux安装字体,si ...

  9. python爬虫有多少种方式_python爬虫-----Python访问http的几种方式

    爬取页面数据,我们需要访问页面,发送http请求,以下内容就是Python发送请求的几种简单方式: 会使用到的库  urllib   requests 1.urlopen import urllib. ...

最新文章

  1. java gettext_JAVA中getText()怎么从一个JTextArea中读出内容?
  2. 女生参加web前端培训压力大吗
  3. 广州企业“掘金”物联网蓝海
  4. 入门深度学习的必备数学知识
  5. java 之 面向对象
  6. 大数据日知录要点整理
  7. 全面收紧!继新加坡后,又一地拟暂停数据中心建设
  8. Linux中的SELinux与chcon以及Samba实现【转】
  9. python垃圾分类图像识别算法_Python 实现一个简单的垃圾分类小游戏(已获校级二等奖)...
  10. linux CentOS7最小化安装环境静默安装Oracle11GR2数据库(安装依赖包_03)
  11. 【Leetcode】98. 验证二叉搜索树
  12. bootstrap多模态框
  13. mysql字符串查询_mysql字符串查询常用命令
  14. win7简单命令让PC变身无线热点,共享M9上网
  15. 我理解的myisam引擎之六 MYI、MYD文件的解析
  16. svg之defs以及use的使用
  17. 怎样做项目计划(转载)
  18. nvidia_tao实现lpr训练
  19. Ubuntu启动项管理
  20. 200多个JS小技巧

热门文章

  1. 移动开发:android , IOS html5
  2. nginx有10个以上参数rewrite的处理
  3. SQL Server 存储过程的应用
  4. 《碟中谍4:幽灵协议》蓝光1080P 720P首发!!汤姆克鲁斯主演
  5. Work measurement II
  6. colab如何通过<>来直接加入相对应的代码段呢?模块化代码操作,真好
  7. air display的实践
  8. 山重水复疑无路,柳暗花明又一村
  9. Final Cut Pro模版网站
  10. UNITY polygon collider不随物体旋转