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. 【LeetCode】前缀树 trie(共14题)

    [208]Implement Trie (Prefix Tree) (2018年11月27日) 实现基本的 trie 树,包括 insert, search, startWith 操作等 api. 题 ...

  2. mysql 注入绕过小特性

    1. 注释 Select /*多行(单行)注释*/ version(); Select version(); #单行注释 Select version(); -- 单行注释 (两划线之后必须有空格) ...

  3. Centos 的防火墙(firewalld,iptables)

    Centos系统防火墙介绍 概述: 1.Filewalld(动态防火墙)作为redhat7系统中变更对于netfilter内核模块的管理工具: 2.iptables service 管理防火墙规则的模 ...

  4. MAN PVCREATE

    PVCREATE(8)                                                        PVCREATE(8) NAME/名称       pvcreat ...

  5. JUC并发工具类

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11449367.html java.util.concurrent及其子包,集中了Java并发的各种基础 ...

  6. hdu 1693 : Eat the Trees 【插头dp 入门】

    题目链接 题意: 给出一个n*m大小的01矩阵,在其中画线连成封闭图形,其中对每一个值为1的方格,线要恰好穿入穿出共两次,对每一个值为0的方格,所画线不能经过. 参考资料: <基于连通性状态压缩 ...

  7. LDD3 第15章 内存映射和DMA

    本章内容分为三个部分: 第一部分讲述了mmap系统调用的实现过程.将设备内存直接映射到用户进程的地址空间,尽管不是所有设备都需要,但是能显著的提高设备性能. 如何跨越边界直接访问用户空间的内存页,一些 ...

  8. [tyvj]P1939玉蟾宫[单调栈]

    [tyvj]P1939 玉蟾宫 ——!x^n+y^n=z^n 背景 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. 描述 这 ...

  9. 开发zeroc ice应用入门(java开发ice应用,python开发ice应用,java与python结合开发ice服务)

    ice作为一种rpc框架,为主流平台设计,包括Windows和Linux,支持广泛的语言,包括C++,Java,C#(和其他.Net的语言,例如Visual Basic),Python,Ruby,PH ...

  10. SpringMVC-设计模式

    MVC 设计不仅限于 Java Web 应用,还包括许多应用,比如前端.PHP..NET 等语言.之所以那么做的根本原因在于解耦各个模块. MVC 是 Model.View 和 Controller ...