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 ...
随机推荐
- MySQL不能连接本地数据库10061
可能的原因是本地服务器没有启动,在安装配置MySQL时,我去掉了开机自动开启,所以开机之后出现了错误10061 解决办法: 一.计算机右击选择管理 二.选择服务,找到MySQL,右击手动,选择启动服务
- 使用 adb 命令一次性为多个设备安装 apk
使用 adb 命令一次性为多个设备安装 apk 原创 2016年07月15日 10:40:53 3154 命令简介 adb install [-lrtsdg] (file) 把包文件推送到设备上并安装 ...
- appium 元素定位与操作:
一.常用识别元素的工具 uiautomator:Android SDK自带的一个工具,在tools目录下 monitor:Android SDK自带的一个工具,在tools目录下 Appium I ...
- 剑指offer-包含min函数的栈20
题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). class Solution: def __init__(self): self.st ...
- [递推+矩阵快速幂]Codeforces 1117D - Magic Gems
传送门:Educational Codeforces Round 60 – D 题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...
- Alpha阶段中间产物
空天猎功能说明书:https://git.coding.net/liusx0303/Plane.git 空天猎代码控制:https://coding.net/u/MR__Chen/p/SkyHunte ...
- 【IdentityServer4文档】- 整体情况
整体概况 大多数现代应用程序看起来或多或少像这样: 最常见的交互是: 浏览器与 Web 应用程序进行通信 Web 应用程序与 Web API 进行通信(有时是Web应用程序自己发起,有时代表用户发起) ...
- java的参数传递
1按值传递:传递的是原始值的副本,而不是原始值的内存地址 基本数据类型是传原始值的副本 class Test02 { public static void main(String[] args) { ...
- <Android>spinner/AutoCompleteTextView绑定适配器
position = (Spinner)findViewById(R.id.position); String[] str = {"CEO","CFO",&qu ...
- CentOS 7 开放防火墙端口
我:最近在使 CentOS 7时发现在本地不能访问linux上8080端口,以上是我的操作,修改后访问成功 CentOS 7 开放防火墙端口 命令 最近公司新的server要求用CentOS7, 发现 ...