2016 Multi-University Training Contest 1 T4
http://acm.hdu.edu.cn/showproblem.php?pid=5726
求不修改区间gcd可以用线段树或者倍增。
求l-n的我们注意观察gcd(al,al+1,...,ar),当l固定不动的时候,r=l...n时,我们可以容易的发现,随着r的増大,gcd(al,al+1,...,ar)是递减的,同时gcd(al,al+1,...,ar)最多 有log 1000,000,000个不同的值,因为al最多也就有log 1000,000,000个质因数。
然后我们用链表记录所有gcd改变的点,这些点将l...n这一段分成若干个相同gcd的区间。由l...n的gcd关系可以推出l-1...n的gcd关系。相邻区间gcd相同时将两个区间合并。用map统计gcd是x的区间有多少个。
#include<cstdio>
#include<cstring>
#include<map>
#define N 100010
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
int te,l[N*],r[N*],a[N*],f[N],x[N];
long long j,n,nex[N];
int t,m,ans,ll,rr,num,i,_;
map<int,long long> sc; int gcd(int aa,int bb)
{
int tt,a=aa,b=bb;
while (a%b!=)
{
tt=a;
a=b;
b=tt%b;
}
return b;
}
void build(int s,int ll,int rr)
{
l[s]=ll;r[s]=rr;
if (ll==rr)
{
scanf("%d",&x[++num]);
a[s]=x[num];
}
else
{
build(s*,ll,(ll+rr)/);
build(s*+,(ll+rr)/+,rr);
a[s]=gcd(a[s*],a[s*+]);
}
}
void sea(int s)
{
if (l[s]>rr || r[s]<ll) return;
if (ll<=l[s] && rr>=r[s])
if (ans==-)
ans=a[s];
else
ans=gcd(ans,a[s]);
else
{
sea(s*);
sea(s*+);
}
}
int main()
{
scanf("%d",&_);
while (_--)
{
printf("Case #%d:\n",++te);
num=;
mem(f);sc.clear();mem(nex);mem(l);mem(r);mem(a);
scanf("%d",&n);
build(,,n);
f[n]=x[n];sc[x[n]]++;
for (i=n-;i>=;i--)
{
j=i+;
while (j!=)
{
f[j]=gcd(f[j],x[i]);
j=nex[j];
}
f[i]=x[i];
nex[i]=i+;
j=i;
while (nex[j]!=)
{
if (f[j]==f[nex[j]])
nex[j]=nex[nex[j]];
else
j=nex[j];
}
j=i;
while (j!=)
{
if (nex[j]==)
sc[f[j]]+=n-j+;
else
sc[f[j]]+=nex[j]-j;
j=nex[j];
} }
scanf("%d",&m);
for (t=;t<=m;t++)
{
scanf("%d%d",&ll,&rr);
ans=-;
sea();
printf("%d %I64d\n",ans,sc[ans]);
}
}
return ;
}
| Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
| 17643445 | 2016-07-20 14:35:10 | Accepted | 5726 | 1950MS | 14112K | 1557B | G++ | lbz007 |
2016 Multi-University Training Contest 1 T4的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 2016 Al-Baath University Training Camp Contest-1 E
Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- 2016 Al-Baath University Training Camp Contest-1 J
Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...
- 2016 Al-Baath University Training Camp Contest-1 I
Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...
- 2016 Al-Baath University Training Camp Contest-1 H
Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...
- 2016 Al-Baath University Training Camp Contest-1 G
Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...
- 2016 Al-Baath University Training Camp Contest-1 F
Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...
- 2016 Al-Baath University Training Camp Contest-1 D
Description X is well known artist, no one knows the secrete behind the beautiful paintings of X exc ...
随机推荐
- mvn相关介绍和命令
1.前言 Maven,发音是[`meivin],"专家"的意思.它是一个很好的项目管理工具,很早就进入了我的必备工具行列,但是这次为了把project1项目完全迁移并应用maven ...
- 转-Zeus资源调度系统介绍
摘要: 本文主要概述阿里巴巴Zeus资源调度系统的背景和实现思路. 本文主线:问题.解决方案.依赖基础知识.工程实践.目标.经验分享.立足企业真实问题.常规解决策略,引出依赖的容器技术.实践方案,所有 ...
- JavaScript的数据类型有哪些?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Typescript - 类型断言
原文:TypeScript基本知识点整理 零.序言 类型断言,可以用来手动指定一个值的类型. 给我的感觉,和 java 中的强制类型转换很像. 常常和联合类型配合使用,如: // 错误示例 funct ...
- 对Vue为什么不支持IE8的解释之一
在JavaScript对象中有一个Object.defineProperties(obj, props)方法 该方法主要用来给指定对象添加自定义属性 可以接收两个参数: 第一个参数 要定义或者修改属性 ...
- 在MySql中如何定义像Java中类型的Boolean类型
在MySql中如何定义像Java中类型的Boolean类型数据..其实,mysql中 是没有直接定义成Boolean这种数据类型.它只能定义成 tinyint(1) ;如果长度是1,tinyint(1 ...
- Windows XP系列全下载(均为MSDN原版)
正版windows xp sp3 下载大全(附:正版密钥) 微软MSDN Windows XP Professional下载 Windows XP Professional 简体中文 (最原始版本,无 ...
- js作用域其二:预解析
文章目錄 解析机制 JavaScript是一门解释型的语言 , 想要运行js代码需要两个阶段 编译阶段: 编译阶段就是我们常说的JavaScript预解析(预处理)阶段,在这个阶段JavaScript ...
- Vue数据绑定(一)
Contents Vue作为当下炙手可热的前端三大框架之一,一直都想深入研究一下其内部的实现原理,去学习MVVM模式的精髓.如果说MVVM是当下最流行的图形用户界面开发模式,那么数据绑定则是这一模式的 ...
- 推荐系统--隐语义模型LFM
主要介绍 隐语义模型 LFM(latent factor model). 隐语义模型最早在文本挖掘领域被提出,用于找到文本的隐含语义,相关名词有 LSI.pLSA.LDA 等.在推荐领域,隐语义模型也 ...