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:
- there is integer j (l ≤ j ≤ r), such that all integers al, al + 1, ..., ar are divisible by aj;
- 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.
Example
5
4 6 9 3 6
1 3
2
5
1 3 5 7 9
1 4
1
5
2 3 5 7 11
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).
要找个最长区间,使得区间里存在一个数x,区间其他数都能被它整除
对于一个i,直接暴力O(n)找左右两边能被a[i]整除的最远的l,r
同时,如果找到了一个i的[l,r],那么对于所有i<j<=r,a[i] | a[j],所以a[j]的对应[l,r]不会比i的更优
所以做完 i 之后可以直接 i 跳到 r+1 的位置
这样还挺快啊?
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 using namespace std; 18 inline LL read() 19 { 20 LL x=0,f=1;char ch=getchar(); 21 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 22 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 23 return x*f; 24 } 25 int n; 26 int a[300010]; 27 int len,tot; 28 int ans[300010]; 29 int main() 30 { 31 n=read(); 32 for (int i=1;i<=n;i++)a[i]=read(); 33 for (int i=1;i<=n;i++) 34 { 35 int l=i,r=i; 36 while (l>=1&&a[l]%a[i]==0)l--;l++; 37 while (r<=n&&a[r]%a[i]==0)r++;r--; 38 if (r-l>len){len=r-l;tot=1;ans[tot]=l;} 39 else if (r-l==len)ans[++tot]=l; 40 i=r; 41 } 42 printf("%d %d ",tot,len); 43 for(int i=1;i<=tot;i++)printf("%d ",ans[i]); 44 }