https://vjudge.net/problem/CodeForces-359D

http://codeforces.com/problemset/problem/359/D

题目大意:

给一串数,问一个区间内所有的数是否能被其其中一个数所全部整除,求出满足条件的区间的长度最大值,并输出这样的区间的个数与它们的左端点。

换句话将,求区间GCD=区间MIN的最大长度区间。

明显st表解决。

对于最大区间长度,二分判断即可。

(因为在poj做过类似的题所以思路能很快……就是题看不懂有点难,所以特地给出翻译)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=1e5*+;
inline int read(){
int X=,w=; char ch=;
while(ch<'' || ch>'') {w|=ch=='-';ch=getchar();}
while(ch>='' && ch<='') X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
inline int qpow(int a){
if(a==)return ;
return (<<a);
}
int gcdd[N][];
int minn[N][];
int log[N];
int n;
int gcd(int a,int b){
if(!b)return a;
return gcd(b,a%b);
}
void st(){
for(int j=;j<=log[n];j++){
for(int i=;i<=n;i++){
int p=i+qpow(j)-;
if(p>n)continue;
gcdd[i][j]=gcd(gcdd[i][j-],gcdd[i+qpow(j-)][j-]);
minn[i][j]=min(minn[i][j-],minn[i+qpow(j-)][j-]);
}
}
return;
}
bool pan(int k){
for(int i=;i<=n;i++){
int j=i+k;
if(j>n)break;
int l=log[k];
int p=qpow(l);
int GCD=gcd(gcdd[i][l],gcdd[j-p+][l]);
int MIN=min(minn[i][l],minn[j-p+][l]);
if(GCD==MIN)return ;
}
return ;
}
int len;
void erfen(int l,int r){
if(l>r)return;
int mid=(l+r)>>;
if(pan(mid)){
len=mid;
erfen(mid+,r);
}else{
erfen(l,mid-);
}
return;
}
vector<int>ans;
int main(){
n=read();
log[]=-;
for(int i=;i<=n;i++){
gcdd[i][]=minn[i][]=read();
log[i]=log[i>>]+;
}
log[]=;
st();
erfen(,n-);
for(int i=;i<=n;i++){
int j=i+len;
if(j>n)break;
int l=log[len];
int p=qpow(l);
int GCD=gcd(gcdd[i][l],gcdd[j-p+][l]);
int MIN=min(minn[i][l],minn[j-p+][l]);
if(GCD==MIN)ans.push_back(i);
}
printf("%d %d\n",(int)ans.size(),len);
for(int i=;i<ans.size();i++){
printf("%d ",ans[i]);
}
return ;
}

CF359D:Pair of Numbers——题解的更多相关文章

  1. cf359D Pair of Numbers

    Simon has an array a1, a2, ..., an, consisting of n positive integers. Today Simon asked you to find ...

  2. CF359D Pair of Numbers gcd+暴力

    利用区间 gcd 个数不超过 log 种来做就可以了~ code: #include <bits/stdc++.h> #define N 300005 #define setIO(s) f ...

  3. Codeforces 395 D.Pair of Numbers

    D. Pair of Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #209 (Div. 2) D. Pair of Numbers (模拟)

    D. Pair of Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. CF55D Beautiful numbers 题解

    题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...

  6. Pair of Numbers

    Codeforces Round #209 (Div. 2) D:http://codeforces.com/contest/359/problem/D 题意:给以一个n个数的序列,然后问你最大的区间 ...

  7. Hdoj 1905.Pseudoprime numbers 题解

    Problem Description Fermat's theorem states that for any prime number p and for any integer a > 1 ...

  8. Hdoj 1058.Humble Numbers 题解

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

  9. [LeetCode] Add Two Numbers题解

    Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...

随机推荐

  1. [JSON].connectionValue()

    语法: [JSON].connectionValue() 说明: 将对象的所有键值接连成新的字符串值 返回: [String] 示例: Set a = toJson() c = Array(1,2,3 ...

  2. 对HashMap进行排序

    首先来看看Map集合获取元素的三种常见方法keySet().values().entrySet() 1. values():返回map集合的所有value的Collection集合(于集合中无序存放) ...

  3. 几个常见移动平台浏览器的User-Agent

    之前介绍的手机站跳转url的一片文稿中提到,依据User Agent判断终端的方法.(文章地址:http://www.cnblogs.com/dereksunok/p/3664169.html ) 若 ...

  4. jQuery官网plugins栏目下那些不错的插件

    前言: 很久以前就关注过jQuery官网plugins栏目下那些全是英文的插件,本人的英文水平很菜,想要全部看懂确实是件不易之事. 好在大部分的案例中都有 view-homepage 或 Try a ...

  5. clientHeight、offsetHeight、scrollHeight、clientTop、scrollTop、offsetTop的对比

    首先,这些都是dom节点的属性. 高宽属性:clientHeight:html元素不含border的高度. 对于box-sizing不同的情况,有些地方需要注意一下.当box-sizing为conte ...

  6. AttributeError: 'TimeLimit' object has no attribute 'monitor'

    原报错代码部分: env.monitor.start(monitor_path, resume=True, video_callable=lambda count: count % record_vi ...

  7. 家用甲醛pm2.5温湿度传感器实验

    最近在装修房子,刷完墙漆铺完了木地板以后,屋里边有很大的味,所以就买了 攀藤科技的PMS5003ST G5ST PM2.5激光粉尘甲醛温湿度三合一传感器,打算自己测一下甲醛浓度,看看什么时候能够入住. ...

  8. mysql 复杂查询

    1.同一个表下多次查询: sql语句: select b.* ,(select name from exh_common.medicine_type a where b.p_id = a.id) as ...

  9. java中 i = i++和 j = i++ 的区别

    由于i++和i--的使用会导致值的改变,所以在处理后置的++和--的时候,java的编译器会重新为变量分配一块新的内存空间,用来存放原来的值, 而完成赋值运算之后,这块内存会被释放. (1)对于j = ...

  10. JSP传递数组给JS的方法

    由于JSP页面的数组无法直接传到JS.所以采用以下方法来获取数组. <% String[] title = { "姓名 ", "学号 ", "性 ...