[codeforces] 359D Pair of Numbers
原题
RMQ st表棵题
要想让一个区间里的所有数都可以整除其中一个数,那么他一定是这个区间内的最小值,并且同时是这个区间的gcd。然后这个问题就转化成了RMQ问题。
维护两个st表,分别是最小值和gcd,然后二分最长区间长度,用st表check即可。
#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 300010
using namespace std;
int n,a[N],mn[N][20],gd[N][20],l,r,mid,cnt,ans[N],p;
int read()
{
int ans=0,fu=1;
char j=getchar();
for (;(j<'0' || j>'9') && j!='-';j=getchar()) ;
if (j=='-') fu=-1,j=getchar();
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
int gcd(int x,int y)
{
return !y?x:gcd(y,x%y);
}
int check(int x)
{
int p=log2(x),m,g,cct=0;
if (p<0) return 0;
for (int i=1;i+x<=n;i++)
{
m=min(mn[i][p],mn[i+x-(1<<p)+1][p]);
g=gcd(gd[i][p],gd[i+x-(1<<p)+1][p]);
if (m==g) ans[++cct]=i;
}
return cct;
}
int main()
{
while (~scanf("%d",&n))
{
r=n;
l=0;
cnt=0;
for (int i=1;i<=n;i++) a[i]=read(),mn[i][0]=gd[i][0]=a[i];
for (int i=1;i<20;i++)
for (int j=1;j+(1<<(i-1))<=n;j++)
{
mn[j][i]=min(mn[j][i-1],mn[j+(1<<(i-1))][i-1]);
gd[j][i]=gcd(gd[j][i-1],gd[j+(1<<(i-1))][i-1]);
}
while (l!=r)
{
mid=(l+r+1)>>1;
p=check(mid);
if (p) l=mid,cnt=p;
else r=mid-1;
}
if (!l)
{
cnt=n;
for (int i=1;i<=n;i++)
ans[i]=i;
}
printf("%d %d\n",cnt,l);
for (int i=1;i<=cnt;i++)
printf("%d%c",ans[i]," \n"[i==cnt]);
}
return 0;
}
[codeforces] 359D Pair of Numbers的更多相关文章
- CodeForces 359D Pair of Numbers (暴力)
题意:给定一个正整数数组,求最长的区间,使得该区间内存在一个元素,它能整除该区间的每个元素. 析:暴力每一个可能的区间,从数组的第一个元素开始考虑,向两边延伸,设延伸到的最左边的点为l, 最右边的点为 ...
- Codeforces 359D Pair of Numbers | 二分+ST表+gcd
题面: 给一个序列,求最长的合法区间,合法被定义为这个序列的gcd=区间最小值 输出最长合法区间个数,r-l长度 接下来输出每个合法区间的左端点 题解: 由于区间gcd满足单调性,所以我们可以二分区间 ...
- 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 ...
- [codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...
- CodeForces 359D (数论+二分+ST算法)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...
- cf359D Pair of Numbers
Simon has an array a1, a2, ..., an, consisting of n positive integers. Today Simon asked you to find ...
- CodeForces 628 D Magic Numbers 数位DP
Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...
- CodeForces - 1245A Good ol' Numbers Coloring (思维)
Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...
随机推荐
- LeetCode567. Permutation in String
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- JAVA / MySql 编程——第八章 DAO 模式
1. 数据持久化:将程序中的数据在瞬时状态和持久状态间转换的机制即为数据持久化: 2. 持久化的实现方式:数据库.普通文件.XML文件: 3. JDBC封装: ...
- 【Effective C++ 读书笔记】导读 Introduction
学习程序语言根本大法是一回事,学习如何以某种语言设计并实现高效程序则是另一回事. 一组明智选择并精心设计的classes.functions.templates可使程序编写容易.直观.高效.并且远离错 ...
- SQL-批量插入和批量更新
批量插入 表结构一样或类似 如果两张表的结构一样,例如一个表的结构和另一个表的结构一样,只是其中一张是临时表,而另一张表是存储数据的表,我们需要进行一次表的迁移的话,我们可以这样. insert in ...
- Small Talk Matters【闲谈很重要】
Small Talk Matters We' ve all been there: in a lift, in line at the bank or on an airplane, 我们都有过这样的 ...
- 删除警告的方法 python
import warningswarnings.filterwarnings('ignore')
- POJ :3614-Sunscreen
传送门:http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- 遗传算法 | Java版GA_TSP(我的第一个Java程序)
嗯哼,第一次写博客,准确说是第一次通过文字的方式记录自己的工作,闲话少叙,技术汪的博客就该直奔技术主题(关于排版问题,会在不断写博客的过程中慢慢学习,先将就着用吧,重在技术嘛~~~). 遗传算法(Ge ...
- python-13常用内建模块
1-datetime #1-获取当前日期和时间 from datetime import datetime now = datetime.now() #当前时间 print(now) #2015-05 ...
- js简单的获取与输出
js获取标签内容和输出内容到页面 获取: html: <select id="choiceSelect" onchange="changeImg()"&g ...