Given an array A1,A2...AN, you have to print the size of the largest contiguous subarray such that
LCM of all integers in that subarray is equal to the product of all integers in that subarray.

Formally,
For a subarray Ai,Ai+1...Aj where 1 ≤ i < j ≤ N to be valid: LCM(Ai,Ai+1...Aj) should be equal to Ai*Ai+1*...*Aj. You have to print the size of the largest valid subarray.

If no subarray satisfies output -1.

Note:A single element is not considered a subarray according to definition in this problem.

Input

First line contains T, the number of testcases. Each testcase consists of N in one line followed by N integers in next line.

Output

For each testcase, print the required answer in one line.

Constraints

  • 1T50
  • 2N105
  • 1Ai106

Example

Input:
3
2
7 2
4
2 2 3 4
3
2 2 4 Output:
2
2
-1

Explanation

Example case 1.LCM(2,7)=2*7. Therefore, subarray A1 to A2 satisfies.

Example case 2.The subarrays A2 to A3 and A3 to A4 are the maximum size possible.

Example case 3.No subarray will satisfy.

Warning: Use fast input/output. Large input files. Solutions may not pass in slower languages.

Update: Time limit for python=10s

给定序列, 求最长连续序列使得 lcm( Ax, ..... Ay ) = Ax*Ax+1*....*Ay .

满足要求的时候 , Ax ~ Ay 这些数要符合, 他们的质因子没有重复。

NlogN预处理质因子,dp出那个最右边的位置即可更新出答案 。~

#include <bits/stdc++.h>
using namespace std;
const int N = ;
const int M = ;
int n,e[N],pos[N],ans[N],to[M],f[M];
bool not_pri[M] ; void init() {
int tot = ;
for( int i = ; i < M ; ++i ) if( !not_pri[i] ) {
to[i] = ++tot; f[i] = i;
for( int j = i + i ; j < M ; j += i ){
not_pri[j] = true ; f[j] = i;
}
}
} int Work( int num , int idx ) {
int res = ;
while( num > ){
int tmp = f[num];
if( pos[ to[tmp] ] ) res = max( res , pos[to[tmp]] );
pos[ to[tmp] ] = idx ;
while( num % tmp == ) num /= tmp;
}
return res ;
} void Run() {
scanf("%d",&n);
for( int i = ; i <= n ; ++i ) scanf("%d",&e[i]);
memset( pos , , sizeof pos );
for( int i = ; i <= n ; ++i ) {
ans[i] = max( ans[i-] , Work( e[i] , i ) );
}
int res = ;
for( int i = ; i <= n ; ++i ) res = max( res , i - ans[i] );
if( res <= ) puts("-1");
else printf("%d\n",res);
} int main()
{
// freopen("in.txt","r",stdin);
init();
int _ , cas = ;
scanf("%d",&_);
while(_--)Run();
}

CodeChef Little Elephant and Balance的更多相关文章

  1. codechef Little Elephant and Permutations题解

    The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...

  2. CodeChef Little Elephant and Movies [DP 排列]

    https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...

  3. CodeChef Little Elephant and Mouses [DP]

    https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中 ...

  4. codechef Little Elephant and Bombs题解

    The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...

  5. CodeChef:Little Elephant and Colored Coins

    类似墨墨的等式 设f[2][j][k]表示a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数 这是个无限循环背包,用spfa优化 #include<cs ...

  6. scau 2015寒假训练

    并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...

  7. CodeChef - LEMOVIE Little Elephant and Movies

    Read problems statements in Mandarin Chineseand Russian. Little Elephant from Zoo of Lviv likes to w ...

  8. 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1288  Solved: 490 ...

  9. Sample a balance dataset from imbalance dataset and save it(从不平衡数据中抽取平衡数据,并保存)

    有时我们在实际分类数据挖掘中经常会遇到,类别样本很不均衡,直接使用这种不均衡数据会影响一些模型的分类效果,如logistic regression,SVM等,一种解决办法就是对数据进行均衡采样,这里就 ...

随机推荐

  1. uniq 去除重复行

    1.命令功能 uniq可以输出或忽略文件中的重复行,经常需要使用sort先对文件进行排序,然后使用uniq去重并计数. 2.语法格式 uniq  option  input uniq   选项    ...

  2. basename 显示文件名或目录名

    1. 命令功能 basename 显示文件名或目录名,不显示文件的全路径文件名 2. 语法格式 basename  文件路径名 3. 使用范例 [root@localhost data]# basen ...

  3. windows 2003 系统管理员debug

  4. PHP Session 序列化及反序列化处理器设置使用不当带来的安全隐患(转)

    PHP Session 序列化及反序列化处理器设置使用不当带来的安全隐患 时间 2014-11-14 15:05:49  WooYun知识库 原文  http://drops.wooyun.org/t ...

  5. nginx之安装

    1.简介 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,供俄 ...

  6. php array_fill()函数 语法

    php array_fill()函数 语法 作用:用键值填充数组.大理石平台价格 语法:array_fill(index,number,value) 参数: 参数 描述 index 必需.被返回数组的 ...

  7. BZOJ 3930: [CQOI2015]选数 莫比乌斯反演 + 杜教筛

    求 $\sum_{i=L}^{R}\sum_{i'=L}^{R}....[gcd_{i=1}^{n}(i)==k]$   $\Rightarrow \sum_{i=\frac{L}{k}}^{\fra ...

  8. 【HDOJ6646】A + B = C(模拟)

    题意 1<=a,b,c<=1e100000 思路: #include<bits/stdc++.h> using namespace std; typedef long long ...

  9. Codeforces Round #585 (Div. 2) E. Marbles (状压DP)

    题目:https://codeforc.es/contest/1215/problem/E 题意:给你一个序列,你可以交换相邻的两个数,要达到一个要求,所有相同的数都相邻,问你交换次数最少是多少 思路 ...

  10. 笨办法学Python(learn python the hard way)--练习程序31-35

    下面是练习31-练习35,基于python3 #ex31.py 1 print("You enter a dark room witn two doors. Do you go throug ...