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. [转]使用flask实现mock server

    什么是mock server: http://www.testclass.net/interface/mock_server 使用flask 实现  mock server : http://www. ...

  2. 2018-10-10-weekly

    Algorithm 字典序排数 What 给定一个整数n,返回从1到n的字典顺序,例如,给定 n =13,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] ,尽可能的优化算法的时间 ...

  3. bzoj3589 动态树 树链剖分+容斥

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3589 题解 事件 \(0\) 不需要说,直接做就可以了. 事件 \(1\) 的话,考虑如果直接 ...

  4. phpstorm 调试时浏览器显示The requested resource / was not found on this server

    1.进入thinkphp项目的public 目录运行以下命令即可 root@jiang:/var/www/tp5# php -S localhost:8080 router.php PHP 7.2.2 ...

  5. 01.python对象

    标准类型 数字 Integer 整型 Boolean 布尔型 Long integer 长整型 (python2) Floating point real number 浮点型 Complex num ...

  6. POJ 3784 Running Median (动态中位数)

    题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方 ...

  7. 033:DTL常用过滤器(2)

    date过滤器: date过滤器:将一个日期按照指定的格式,格式化成字符串.示例代码如下: views.py: from datetime import datetime def cur_date(r ...

  8. 【HbuilerX-Bug】终端无法显示打印信息,也无法输入

    经过调试HbuilderX“终端”插件,最终定位问题,问题是插件在打开终端时,无法定位具体的窗口程序,如“cmd.exe”.“powershell.exe”等.可能产生原因:1.可能是电脑系统升级产生 ...

  9. ht-4 hashmap特性

    一.hashmap底层原理: hashmap调用默认构造方法会产生一个默认底层是长度为16的Entry数组,首先调用key的hasCode()方法来得到一个整数, int hash = hash(ke ...

  10. 实现粘贴WORD图片的在线编辑器

    我司需要做一个需求,就是使用富文本编辑器时,不要以上传附件的形式上传图片,而是以复制粘贴的形式上传图片. 在网上找了一下,有一个插件支持这个功能. WordPaster 安装方式如下: 直接使用Wor ...