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. vue.js 笔记

    <!-- 多层for循环 --> <ul> <li v-for="(ite,key) in list2"> {{key}}-------{{it ...

  2. eclipse没有Web项目和Server选项

    (1)在Eclipse中菜单help选项中选择install new software选项 (2)在work with 栏中输入 http://download.eclipse.org/release ...

  3. RabbitMQ拓展学习 自定义配置RabbitMQ连接属性

    最近研究RabbitMQ从本地获取配置,主要场景是RabbitMQ的连接配置如:ip地址这些需要从外部的配置服务器获取.面对这个问题,有两个解决方案,一个是用RabbitMQ原生的连接方式,但是如果使 ...

  4. java并发学习--第一章 线程的创建

    所谓的并发就是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行.所以我们看似几个线程在同时进行,其实在操作系统中 ...

  5. alert(1) to win 11

  6. python 日期生成和时间格式化

    记录下日期时间的转换和生成:1.这个是使用datetime 函数生成往后几天的时间,比如当前日期是2019-07-01 那么控制days=1然后再和当前的时间相加,就能得到明天的日期def time_ ...

  7. size_t为什么重要

    参考:https://www.zhihu.com/question/24773728/answer/66535663 前言:使用size_t可能会提高代码的可移植性.有效性或者可读性,或许同时提高这三 ...

  8. pyhive连接hive(失败)

    一.安装pyhive pip install sasl(需要来下载至本地安装:https://download.lfd.uci.edu/pythonlibs/q4hpdf1k/sasl-0.2.1-c ...

  9. CodeChef Max-digit Tree(动态规划)

    传送门. 题解: 最主要的问题是如何判断一个数是否合法,这就需要发现性质了. 这个状态划分还是不太容易想到, 每次加的数\(∈[0,k)\),也就是个位一直在变变变,更高的位每次都是加一,这启发我们状 ...

  10. 【Java】Java中charAt()方法的使用

    说明 java.lang.String.charAt() 方法返回指定索引处的char值.索引范围是从0到length() - 1.对于数组索引,序列的第一个char值是在索引为0,索引1,依此类推 ...