D. Pair of Numbers
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Simon has an array a1, a2, ..., an, consisting of n positive integers. Today Simon asked you to find a pair of integers l, r (1 ≤ l ≤ r ≤ n), such that the following conditions hold:

  1. there is integer j (l ≤ j ≤ r), such that all integers al, al + 1, ..., ar are divisible by aj;
  2. value r - l takes the maximum value among all pairs for which condition 1 is true;

Help Simon, find the required pair of numbers (l, r). If there are multiple required pairs find all of them.

Input

The first line contains integer n (1 ≤ n ≤ 3·105).

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 106).

Output

Print two integers in the first line — the number of required pairs and the maximum value of r - l. On the following line print all l values from optimal pairs in increasing order.

Examples
Input
5
4 6 9 3 6
Output
1 3
2
Input
5
1 3 5 7 9
Output
1 4
1
Input
5
2 3 5 7 11
Output
5 0
1 2 3 4 5
Note

In the first sample the pair of numbers is right, as numbers 6, 9, 3 are divisible by 3.

In the second sample all numbers are divisible by number 1.

In the third sample all numbers are prime, so conditions 1 and 2 are true only for pairs of numbers (1, 1), (2, 2), (3, 3), (4, 4), (5, 5).

【分析】给定一个正整数数组,求最长的区间,使得该区间内存在一个元素,它能整除该区间的每个元素。

可能我的方法有点复杂吧。我是先从小到大排序,记录所有数的位置,然后从小到大向两边扩展,就行了,详细见代码一。还有种做法类似DP,很短很神奇,见代码二。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
const int N = 3e5+;
const int M = 1e6+;
int n,m,k,tot=,tim=;
int head[N],vis[N],l[N],r[N];
int a[N],b[N];
vector<pair<int,int> >vec;
vector<int>Ans;
struct man{
int l,r,len;
}ans[N];
bool cmp(man s,man d){
return s.len>d.len;
}
void Find(int p) {
vis[p]=;
int ret=;
int u=b[p];
int ll,rr;
++tot;
for(int i=; i<=n; i++) {
if(!l[tot]) {
ll=p-i;
if(ll>=&&b[ll]%u==) {
vis[ll]=;
} else l[tot]=ll+,ret++;
}
if(!r[tot]) {
rr=p+i;
if(rr<=n&&b[rr]%u==) {
vis[rr]=;
} else r[tot]=rr-,ret++;
}
if(ret==)return;
}
}
int main() {
scanf("%d",&n);
for(int i=; i<=n; i++) {
scanf("%d",&a[i]);
b[i]=a[i];
vec.pb(mp(a[i],i));
}
sort(vec.begin(),vec.end());
for(int i=;i<n;i++){
int p=vec[i].second;
if(!vis[p])Find(p);
}
for(int i=; i<=tot; i++) {
ans[i-].l=l[i];
ans[i-].r=r[i];
ans[i-].len=r[i]-l[i];
}
sort(ans,ans+tot,cmp);
int t=ans[].len,ret=;
for(int i=;i<tot;i++){
if(ans[i].len<t)break;
ret++;
}
printf("%d %d\n",ret,t);
for(int i=;i<ret;i++){
Ans.pb(ans[i].l);
}
sort(Ans.begin(),Ans.end());
for(int i=;i<ret;i++){
printf("%d ",Ans[i]);
}
printf("\n");
return ;
}
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e6+;
const int M = 1e6+;
int n,L,R,len,Ans,xllend3;
int a[N],ans_l[N],ans_r[N],ans[N];
void init() {
scanf("%d",&n);
for(int i=; i<=n; ++i)scanf("%d",&a[i]),ans_l[i]=ans_r[i]=i;
}
void work() {
for(int i=; i<=n; ++i)for(; ans_l[i]>&&a[ans_l[i]-]%a[i]==;)ans_l[i]=ans_l[ans_l[i]-];
for(int i=n; i>=; --i)for(; ans_r[i]<n&&a[ans_r[i]+]%a[i]==;)ans_r[i]=ans_r[ans_r[i]+];
for(int i=; i<=n; ++i) {
L=ans_l[i];
R=ans_r[i];
if(R-L==len)ans[++Ans]=L;
if(R-L>len)len=R-L,ans[Ans=]=L;
}
} void outit() {
sort(ans+,ans+Ans+);
for(int i=; i<=Ans; ++i)if(ans[i]!=ans[i-])++xllend3;
printf("%d %d\n%d",xllend3,len,ans[]);
for(int i=; i<=Ans; ++i)if(ans[i]!=ans[i-])printf(" %d",ans[i]);
puts("");
} int main() {
init();
work();
outit();
return ;
}

最新文章

  1. WebAPI2使用AutoFac依赖注入完整解决方案。
  2. 【转】深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接
  3. Java中的static的使用
  4. 使用pl/sql監控PROCEDURE執行時間
  5. UVA 11019 Matrix Matcher(ac自动机)
  6. Mysql 创建数据库表(删除,删除,插入)
  7. POJ 3461 Oulipo(字符串匹配,KMP算法)
  8. jQuery数字加减插件
  9. PHP的Session机制
  10. 修改nopCommerce中的实体
  11. freemark标签从后台接过来数据Boolean在前台还是Boolean输出(四)
  12. JSP自定义简单标签入门之带有属性
  13. [转帖] Linux 时间参数
  14. [转] Javascript 原型链
  15. oi程序提交注意:bool
  16. java.security.NoSuchAlgorithmException: AES KeyGenerator not available
  17. C语言变量的作用域和存储类型
  18. jQuery mobile 初始化页面的过程
  19. 总结day2 ---- while循环的简单使用, 格式化输出.运算符.以及编码的应用
  20. Android多媒体之照相机

热门文章

  1. [洛谷P2763]试题库问题
  2. Intellij Idea debug 远程部署的的tomcat项目
  3. Join EC2 into AD with SSM and remote powershell in AWS
  4. JS遮罩层弹框效果
  5. codevs1163访问艺术馆 树形dp
  6. react+redux基础用法
  7. bzoj 2662&amp;bzoj 2763 SPFA变形
  8. html——零散知识点
  9. HDU1248 (完全背包简单变形)
  10. socket中的函数遇见EINTR的处理【转】