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 ...
随机推荐
- <SDOI2009>学校食堂
我. 调了快一天 因为一个位置错误的感叹号 :-) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 狗dp #include<cstdio> #include<cstr ...
- 将js进行到底:node学习5
HTTP开发之Connect工具集--中间件 继学习node.js的TCP API和HTTP API之后,node.js web开发进入了正轨,但这就好像Java的servlet一样,我们不可能使用最 ...
- IDEA如何自动添加注解作者等信息?
1.点击File 2.点击Settings 3.点击Editor 4.点Live Templates 5.点击左上角加号选中第2个 6.自定义命名,选中你自己创建的组,点击左上角加号选择第1个选项 ...
- 使用 KM 处理 HHKB 方向键
对于上了 HHKB 这条贼船的人来说,刚开始使用起来最大的别扭可能就是没有方向键的问题了. 最早的我使用 Karabiner 来解决,里边有一些内置的组合可以替代方向键,我用 control + hj ...
- 基于webhook方案的Git自动部署方案
之前已经用Git实现了自己博客的提交自动部署,并自动提交到GitHub和coding以备不时之需.平时项目代码都托管在Coding或者GitHub上,也已经用上了coding提供的webhook功能, ...
- 如何通过学校系统漏洞注册到 @edu.cn 邮箱账号?
此文章仅针对我自己学校的系统进行分析,并不代表所有学校的系统都是如此. 我们学校比较"抠",可能是为了节省学校的带宽资源然后禁止学生注册教育邮箱账号.不过像一部电影所说的那样&qu ...
- Centos 7 安装Mysql8 主从同步复制
环境:Centos 7 软件:Mysql8 安装方式:Yum 1.从官网下载最新yum 源对应Cenots 7 版本安装: [root@DataNode-03 ~]# yum -y localinst ...
- 压力测试(三)-自定义变量和CSV可变参数实操
1.Jmeter用户自定义变量实战 简介:什么是用户自定义变量,怎样使用 为什么使用:很多变量在全局中都有使用,或者测试数据更改,可以在一处定义,四处使用 比如服务器地址 1.线程组->add ...
- 7-29 jmu-python-不同进制数 (10 分)
输入一个十进制整数,输出其对应的八进制数和十六进制数.要求采用print函数的格式控制进行输出,八进制数要加前缀0o,十六进制数要加前缀0x. 输入格式: 输入一个十进制整数,例如:10 输出格式: ...
- echart 新手踩坑
仪表盘踩坑 我采用的是单文件引入的方式来加载echarts图标也可以使用配置等方式详情参考文档,如果同学们要做出更加丰富的样式请参考文档配置手册配置手册:http://echarts.baidu.co ...