http://acm.hdu.edu.cn/showproblem.php?pid=5726

求不修改区间gcd可以用线段树或者倍增。

求l-n的我们注意观察gcd(a​l​​,a​l+1​​,...,a​r​​),当l固定不动的时候,r=l...n时,我们可以容易的发现,随着r的増大,gcd(a​l​​,a​l+1​​,...,a​r​​)是递减的,同时gcd(a​l​​,a​l+1​​,...,a​r​​)最多 有log 1000,000,000个不同的值,因为a​l​​最多也就有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的更多相关文章

  1. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  2. 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 ...

  3. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. zabbix的MariaDB数据库改变存储位置

    #创建 mkdir /公司缩写/mysql   #停止数据库服务 systemctl status mariadb.service systemctl stop mariadb.service   # ...

  2. Trailing Zeroes (III) LightOJ - 1138 二分+找规律

    Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! ...

  3. Java web期末项目第一阶段成果发表

    摘要 我们做的系统是一个基于Java web与MySQL的食堂订餐系统 班级: 计科二班 小组成员:李鉴宣.袁超 我们的第一阶段主要完成以下三件事: 完成项目的需求分析 完成项目的领域逻辑(domai ...

  4. 通过virt-manager给Windowsxp系统配置virtio驱动

    在虚拟机的detail上添加一个硬件设备. 下载virtio.iso文件,我使用的版本126,具体的virtio驱动放到了10.2上的itest的目录中,使用可以找. 在配置中,添加virtio硬盘. ...

  5. ajax学习相关

    choices参数 当存在某个字段的值只有有限的几个时,如性别(只有男.女),婚否(已婚.未婚)等,这种情况,我们可以用数字来代替这些中文字符,可以减轻数据库的负担,这时,就需要用到choices参数 ...

  6. API网关Kong

    官网:https://konghq.com/ 各种方式安装汇总:https://konghq.com/install/ 命令列表:https://docs.konghq.com/0.14.x/admi ...

  7. 5G-NR物理信道与调制-下行链路v1.1.0

    上接<5G-NR物理信道与调制v1.1.0>下行链路 References Definitions, symbols and abbreviations 帧结构与物理资源 通用函数 上行链 ...

  8. 使用Navicat或者其他数据库工具连接阿里云EDS(数据库服务器)实例过程详解

    使用Navicat或者其他数据库工具连接阿里云EDS(数据库服务器)实例过程详解 背景:这几天从阿里云上面购买了云服务器,最垃圾的那种,还送oss和EDS数据库服务器,只不过EDS数据库服务器只有一个 ...

  9. java集合-set

    #java集合-set Map用于存储key-value的映射,其中key的值是不能重复的.并且还需要正确的覆写equals方法和hashCode方法 如果我们只需要存储不重复的key,并不需要存储对 ...

  10. grid实战之微信钱包 腾讯服务界面

    网格布局简介 CSS3网格布局是让开发人员设计一个网格并将内容放在这些网格内.而不是使用浮动制作一个网格,实际上是你将一个元素声明为一个网格容器,并把元素内容置于网格中. 有一些浏览器是不支持网格布局 ...