CF359D:Pair of Numbers——题解
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——题解的更多相关文章
- cf359D Pair of Numbers
Simon has an array a1, a2, ..., an, consisting of n positive integers. Today Simon asked you to find ...
- CF359D Pair of Numbers gcd+暴力
利用区间 gcd 个数不超过 log 种来做就可以了~ code: #include <bits/stdc++.h> #define N 300005 #define setIO(s) f ...
- Codeforces 395 D.Pair of Numbers
D. Pair of Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- CF55D Beautiful numbers 题解
题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...
- Pair of Numbers
Codeforces Round #209 (Div. 2) D:http://codeforces.com/contest/359/problem/D 题意:给以一个n个数的序列,然后问你最大的区间 ...
- Hdoj 1905.Pseudoprime numbers 题解
Problem Description Fermat's theorem states that for any prime number p and for any integer a > 1 ...
- Hdoj 1058.Humble Numbers 题解
Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
随机推荐
- macOS 10.13 High Sierra PHP开发环境配置
命令:sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm -rf /Library/StartupItems/MySQLCOM ...
- Grafana学习
一.安装 Grafana最新版本4.3.1安装(后端使用mysql) 二.使用
- 内置方法(item系列)
class Foo: def __init__(self,name): self.name = name def __getitem__(self, item): # 获取时触发 print('get ...
- ubuntu 执行Python脚本出现: /usr/bin/env: ‘python\r’: No such file or directory
原因: #!/usr/bin/env python 在ubuntu会变成 #!/usr/bin/env python\r 而\r 会被shell 当成参数 所以出现: /usr/bin/env: ‘ ...
- 【Linux 运维】 安装PHP工具Composer
一.安装PHP 由于Composer是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的 ...
- CodeForces 908C. New Year and Curling 解题报告 Java
1. 思路 这题实际上是个几何问题——两个外相切的圆,由勾股定理,他们的纵坐标有以下的规律: 则有$$y_{n+1} = y_{n} + \sqrt{(2r)^2 - (x_{n} - x_{n+1} ...
- SpringCloud IDEA 教学 (五) 断路器控制台(HystrixDashboard)
写在开头 断路器控制台是为了查看断路器运行情况而研发的.本章介绍了断路器控制台的搭建,代码基于之前Client的搭建.HystrixDashboard基于之前配置好的,使用了HystrixComman ...
- “Hello world!”团队第一周贡献分分配结果
小组名称:Hello World! 项目名称:空天猎 组长:陈建宇 成员:刘成志.阚博文.刘淑霞.黄泽宇.方铭.贾男男 第一周贡献分分配结果 基础分 会议分 提功能分 个人表现分 各项总分 最终分 ...
- POJ 2823 (滑动窗口)
这道题最容易想到的是用朴素的做法,即 每滑动一次,就遍历一次窗口找出最大最小值,这样时间复杂度为O(n*k),由于题目数据比较大,这种做法肯定是超时的. 另外,根据书上的讲解,还可以采用优先队列来求解 ...
- android入门 — AlertDialog对话框
常见的对话框主要分为消息提示对话框.确认对话框.列表对话框.单选对话框.多选对话框和自定义对话框. 对话框可以阻碍当前的UI线程,常用于退出确认等方面. 在这里主要的步骤可以总结为: 1.创建Aler ...