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等,一种解决办法就是对数据进行均衡采样,这里就 ...
随机推荐
- 两台服务器共享文件Windows和Linux
最近遇到一个问题,新装的两台服务器死活就是挂载不上samba服务器上的文件,在输入mount挂载命令的时候提示只读什么的,尝试了在挂载的时候用只读的方式去挂载也是不行,最终发现了,缺少安装cifs包, ...
- linux--基础知识5
#文件合并与文件归档 #cat /etc/passwd > new_pass.txt (创建一个新的文档并将cat/etc/passwd的内容合并进来) #echo "xxxx&quo ...
- tail 显示文件内容尾部
1.命令功能 tail默认显示文件内容尾部10行. 2.语法格式 tail option file 参数说明 参数 参数说明 -n 显示指定行数 -f 实时输出文件变化后追加的数据 -s 监视文件 ...
- No qualifying bean of type xxx' available 的一种解决方法
获取bean Class beanClass = Class.forName(event.className); FilterEvent filterEvent = (FilterEvent)Bean ...
- json和dict 在requests中请求
上面的问题,在这么晚的夜里解决了 data 接受的是json格式数据, json 接受dict格式点的数据, 这个文章中也讲到了https://www.cnblogs.com/beile/p/1086 ...
- prototype的用法
定义: prototype 属性使您有能力向对象添加属性和方法. 语法: object.prototype.name=value 实例: function prot(){ this.name = 'J ...
- randomForest R 学习笔记
object type randomForest 会根据变量的类型来决定regression或classification.class(iris$Species)是 factor,所以是classif ...
- Slide Window 专题
992. Subarrays with K Different Integers 给定一个正整数数组,计算刚好有K个不同数的子数组的个数.(For example, [1,2,3,1,2] has 3 ...
- 170817关于AJAX的知识点
1.AJAX [1] AJAX简介 全称: Asynchronous JavaScript And XML ...
- [CSP-S模拟测试110]题解
也许是最后一篇了. A.最大或 不错的签到题. 对于二进制位来说,高位的一个1比低位的所有1的贡献总和还要大. 显然,$r$必选,因为$r$中所有1的相对考前.那么考虑如何构造另一个数. 首先$l$和 ...