CodeChef 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
- 1 ≤ T ≤ 50
- 2 ≤ N ≤ 105
- 1 ≤ Ai ≤ 106
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的更多相关文章
- codechef Little Elephant and Permutations题解
The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...
- CodeChef Little Elephant and Movies [DP 排列]
https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...
- CodeChef Little Elephant and Mouses [DP]
https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中 ...
- codechef Little Elephant and Bombs题解
The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...
- CodeChef:Little Elephant and Colored Coins
类似墨墨的等式 设f[2][j][k]表示a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数 这是个无限循环背包,用spfa优化 #include<cs ...
- scau 2015寒假训练
并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...
- CodeChef - LEMOVIE Little Elephant and Movies
Read problems statements in Mandarin Chineseand Russian. Little Elephant from Zoo of Lviv likes to w ...
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
- Sample a balance dataset from imbalance dataset and save it(从不平衡数据中抽取平衡数据,并保存)
有时我们在实际分类数据挖掘中经常会遇到,类别样本很不均衡,直接使用这种不均衡数据会影响一些模型的分类效果,如logistic regression,SVM等,一种解决办法就是对数据进行均衡采样,这里就 ...
随机推荐
- 利用sql语句建立全国省市区三级数据库
一.创建数据库zone CREATE DATABASE IF ONT EXISTS zone; 二.建立省级表并增加数据 DROP TABLE IF EXISTS `provinces`; CREAT ...
- Arrays基本使用
public static void main(String[] args) { String[] a = { "a", "b", "c" ...
- PHP Session 序列化及反序列化处理器设置使用不当带来的安全隐患(转)
PHP Session 序列化及反序列化处理器设置使用不当带来的安全隐患 时间 2014-11-14 15:05:49 WooYun知识库 原文 http://drops.wooyun.org/t ...
- Linux忘记root密码解决方案
忘记Linux root密码时,只需重启Linux系统,然后引导进入Linux的单用户模式(init 1),由于单用户模式不需要输入登陆密码,因此,可直接登陆系统,修改root密码即可解决问题.需要说 ...
- 前端面试题:CSS实现水平垂直居中
这是一个挺常见的前端面试题,但是没有做过总结.有的时候可能会使用完了,很长一段时间不去使用,会慢慢忘记.所以,温故而知新,还是很有必要的. 一.绝对定位元素的居中实现 这一种工作中用的应该是最多的,兼 ...
- Codeforces 512B: Fox And Jumping
题目链接 题意说的是,有n种卡片,使用第i种卡片可以使当前自己在数轴上的位置移动 l[i],要获得使用第i种卡片的代价是 c[i],求能使自己移动到数轴上任意位置的最小代价,如果不可能则输出-1 当前 ...
- tensorflow图像处理函数(1)
1.tensorflow中对jpeg格式图像的编码/解码函数: import matplotlib.pyplot as plt import tensorflow as tf image_raw_da ...
- ht-3 linkedList特性
LinkedList内部封装的是双向链表数据结构,每个节点是一个Node对象. Node对象中封装的是要被添加的元素,还有一个指向上一个Node对象的引用和 指向下一个Node对象的引用 , 与Arr ...
- URL跳转漏洞
URL跳转原理: 由于越来越多的需要和其他第三方应用交互,以及在自身应用内部根据不同的逻辑将用户引向到不同的页面,譬如一个典型的登录接口就经常需要在认证成功之后将用户引导到登录之前的页面,整个过程中如 ...
- HTML计算机代码元素
计算机代码 1 2 3 4 5 6 var person = { firstName:"Bill", lastName:"Gates", ...