CodeChef LUCKYBAL Little Elephant and Balance

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 = 100010;
const int M = 1000010;
int n,e[N],pos[N],ans[N],to[M],f[M];
bool not_pri[M] ;

void init() {
    int tot = 0 ;
    for( int i = 2 ; 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 = 0 ;
    while( num > 1 ){
        int tmp = f[num];
        if( pos[ to[tmp] ] ) res = max( res , pos[to[tmp]] );
        pos[ to[tmp] ] = idx ;
        while( num % tmp == 0 ) num /= tmp;
    }
    return res ;
}

void Run() {
    scanf("%d",&n);
    for( int i = 1 ; i <= n ; ++i ) scanf("%d",&e[i]);
    memset( pos , 0 , sizeof pos );
    for( int i = 1 ; i <= n ; ++i ) {
        ans[i] = max( ans[i-1] , Work( e[i] , i ) );
    }
    int res = 0 ;
    for( int i = 1 ; i <= n ; ++i ) res = max( res , i - ans[i] );
    if( res <= 1 ) puts("-1");
    else printf("%d\n",res);
}

int main()
{
//    freopen("in.txt","r",stdin);
    init();
    int _ , cas = 1 ;
    scanf("%d",&_);
    while(_--)Run();
}

时间: 2024-10-09 16:28:49

CodeChef LUCKYBAL Little Elephant and Balance的相关文章

codechef Little Elephant and Permutations题解

The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numbers 1, 2, ...,N. He calls a permutation A good, if the number of its inversions is equal to the number of its local inversions. The number of inversio

CodeChef Little Elephant and Mouses [DP]

https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中有老鼠,如果大象所在的格子和某个有老鼠的格子的曼哈顿距离$\e$1,大象就会被那只老鼠吓到.求一条移动路径,使得吓到过大象的老鼠数量最少.n;m $\le$100. 开始刷济南集训hzc的课件啦! 最简单的一道$DP$ 一只老鼠只会吓大象一次$f[i][j][0/1]$记录从左还是上走来的,转移时讨

codechef Little Elephant and Bombs题解

The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy buildings placed in a row and numbered from left to right strating from 0. Each building i (except the first and the last) has exactly two adjacent build

CodeChef Little Elephant and Movies [DP 排列]

https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2... pn,求这n个数的所有排列中,激动值不超过k的个数.$1 k \le n \le 200,1 \le pi \le 200$ 这种题有一个很神的想法: 把排列按某种顺序往里插入,使得后不会影响前 对于本题,先离散化去重后,从大到小插入,后插入的元素不会影响已经插入的元素严格大于前面所有数 $f[

&lt;Dr.Elephant&gt;

Why Dr.Elephant? Most of Hadoop optimization tools out there, but they are focused on simplifying the deploy and managment of Hadoop clusters. Very few tools are designed to help Hadoop users optimize their flows. Dr.Elephant supports Hadoop with a v

LB(Load balance)负载均衡集群--{LVS-[NAT+DR]单实例实验+LVS+keeplived实验} 菜鸟入门级

LB(Load balance)负载均衡集群 LVS-[NAT+DR]单实例实验 LVS+keeplived实验 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统. 逻辑可分为: 1调度层 (Director):它是整个集群对外面的前端机,负责将客户的请求发送到一组服务器上执行,而客户认为服务是来自一个IP地址(我们可称之为虚拟IP地址)上的. 2[服务器池(server pool)/集群层(Real server)]:是一组真正执行客

轻量级网络流量转发工具 - Balance

Balance是一款具有负载均衡和故障转移功能的TCP代理软件.支持IPV6地址的监听和转发,支持rr轮询和ip hash. 某些功能可以替代iptables,比如讲来自本地的80端口转发到8080上: iptables是这样写的: iptables -t nat -A PREROUTING -d 192.168.1.1 -p tcp --dport 80 -j DNAT --to 192.168.1.1:8080 balance只需要这样: balance 80 localhost:8080

益计算Shprotification.v6.8.15.22+Heat.Balance.v6.12.27.36+Cold.Balance.v2.6.14.18

热增益计算Shprotification.v6.8.15.22+Heat.Balance.v6.12.27.36+Cold.Balance.v2.6.14.18 Andrey.Shirshov.Shprotification.v6.8.15.22 Andrey.Shirshov.Heat.Balance.v6.12.27.36 Andrey.Shirshov.Cold.Balance.v2.6.14.18   "现代ASHRAE标准参考数据用于从人.设备.确定热输入的人工照明,半透明的击剑系数(

CodeChef FNCS (分块+树状数组)

题目:https://www.codechef.com/problems/FNCS 题解: 我们知道要求区间和的时候,我们用前缀和去优化.这里也是一样,我们要求第 l 个函数到第 r 个函数 [l, r] 的函数和,那么我们可以用 sum[r] - sum[l-1] 来求得. 由于这个数据量有点大,所以我们将函数分块. 例如样例: 1 3 有5个函数,那么我们分成3块.{ [1 3] , [2 5] }, { [4 5], [3 5] }, { [1 2] }.每一块对应都有一个sum ,这时如