Problem Description
Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There are Q(Q≤100,000) queries. For each query l,r you have to calculate gcd(al,,al+1,...,ar) and count the number of pairs(l′,r′)(1≤l<r≤N)such that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar).
 
Input
The first line of input contains a number T, which stands for the number of test cases you need to solve.

The first line of each case contains a number N, denoting the number of integers.

The second line contains N integers, a1,...,an(0<ai≤1000,000,000).

The third line contains a number Q, denoting the number of queries.

For the next Q lines, i-th line contains two number , stand for the li,ri, stand for the i-th queries.

 
Output
For each case, you need to output “Case #:t” at the beginning.(with quotes, t means the number of the test case, begin from 1).
For each query, you need to output the two numbers in a line. The first number stands for gcd(al,al+1,...,ar) and the second number stands for the number of pairs(l′,r′) such that gcd(al′,al′+1,...,ar′) equal gcd(al,al+1,...,ar).
Sample Input
1
5
1 2 4 6 7
4
1 5
2 4
3 4
4 4
Sample Output
Case #1:
1 8
2 4
2 4
6 1
思路:我们注意观察gcd(a​l​​,a​l+1​​,...,a​r​​),当l固定不动的时候,r=l...nr=l...n时,我们可以容易的发现,随着rr的増大,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个质因数所以我们可以在log级别的时间处理出所有的以L开头的左区间的gcd(a​l​​,a​l+1​​,...,a​r​​) 那么我们就可以在n log 1000,000,000的时间内预处理出所有的gcd(a​l​​,a​l+1​​,...,a​r​​)然后我们可以用一个map来记录,gcd值为key的有多少个 然后我们就可以对于每个询问只需要查询对应gcd(a​l​​,a​l+1​​,...,a​r​​)为多少,然后再在map 里面查找对应答案即可.

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
int a[]; vector<pair<int,int> > v[];
map<int,long long>ans; int __gcd(int x,int y)
{
int r=x%y;
x=y;
y=r;
if(r==) return x;
return __gcd(x,y);
} int main()
{
int T,Case=;
int n;
cin>>T;
while(T--)
{
ans.clear();
cin>>n;
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
int tot=;
for(int j=;j<v[i-].size();j++)
{
int s1=v[i-][j].first;
int s2=v[i-][j].second;
int r=__gcd(a[i],s1);
if(tot==r) continue;
tot=r;
v[i].push_back(make_pair(r,s2));
}
if(tot!=a[i]) v[i].push_back(make_pair(a[i],i));
for(int j=;j<v[i].size();j++)
{
if(j+==v[i].size())
ans[v[i][j].first]+=i+-v[i][j].second;
else
ans[v[i][j].first]+=v[i][j+].second-v[i][j].second;
}
}
cout<<"Case #"<<(++Case)<<":"<<endl;
int Q;
cin>>Q;
while(Q--)
{
int i,l,r;
scanf("%d%d",&l,&r);
for(i=;i<v[r].size();i++)
{
if(v[r][i].second>l) break;
}
printf("%d %I64d\n",v[r][i-].first,ans[v[r][i-].first]);
}
for(int i=;i<;i++)
v[i].clear();
}
return ;
}

2016暑假多校联合---GCD的更多相关文章

  1. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

  2. 2016暑假多校联合---Windows 10

    2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...

  3. 2016暑假多校联合---Substring(后缀数组)

    2016暑假多校联合---Substring Problem Description ?? is practicing his program skill, and now he is given a ...

  4. 2016暑假多校联合---To My Girlfriend

    2016暑假多校联合---To My Girlfriend Problem Description Dear Guo I never forget the moment I met with you. ...

  5. 2016暑假多校联合---A Simple Chess

    2016暑假多校联合---A Simple Chess   Problem Description There is a n×m board, a chess want to go to the po ...

  6. 2016暑假多校联合---Another Meaning

    2016暑假多校联合---Another Meaning Problem Description As is known to all, in many cases, a word has two m ...

  7. 2016暑假多校联合---Death Sequence(递推、前向星)

    原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...

  8. 2016暑假多校联合---Counting Intersections

    原题链接 Problem Description Given some segments which are paralleled to the coordinate axis. You need t ...

  9. 2016暑假多校联合---Joint Stacks (STL)

    HDU  5818 Problem Description A stack is a data structure in which all insertions and deletions of e ...

随机推荐

  1. vue初体验:实现一个增删查改成绩单

    前端变化层出不穷,去年NG火一片,今年react,vue火一片,ng硬着头皮看了几套教程,总被其中的概念绕晕,react是faceback出品,正在不断学习中,同时抽时间了解了vue,查看了vue官方 ...

  2. Lua 协程coroutine

    协程和一般多线程的区别是,一般多线程由系统决定该哪个线程执行,是抢占式的,而协程是由每个线程自己决定自己什么时候不执行,并把执行权主动交给下一个线程. 协程是用户空间线程,操作系统其存在一无所知,所以 ...

  3. [Spring框架]Spring AOP基础入门总结一.

    前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...

  4. fir.im Weekly - iOS开发中的Git流程

    本期 fir.im Weekly 收集了微博上的热转资源,包含 Android.iOS 开发工具.源码等好用的轮子,还有一些 APP 设计的 Tips,希望对你有用. 精仿知乎日报 iOS 端 @我偏 ...

  5. settimeout里面函数有无双引号的区别

    在写定时器时很容易搞混,所以记下防止忘记. 双引号中的作用域不捕捉局部变量,不用双引号包着的是捕捉局部作用域 var a = function() { alert(1111) } function a ...

  6. Git使用相关

    Git使用相关 使用git这么久还是时不时碰到小问题,根本原因在于没有仔细研究和做笔记 Git修改remote地址 之前一直使用的ssh的地址,估计是没配置好,每次都需要输密码烦死了,今天看到个用ht ...

  7. 真正的mybatis_redis二级缓存

    网上流传的代码缓存失效存在严重问题. 思路....以后再细说 目前的方案还不够完美,失效力度控制不够细. 主要代码 import java.io.BufferedReader; import java ...

  8. maven+spring+springMVC+mybatis+dubbox

      milestone 2016612  dubbox+spring+mybatis provider调通

  9. Vertex and Fragment Shader

    Semantics语义词: 定义:GPU工作时,数据通常暂存在寄存器,那么在Cg中,语义词就指定了输入/输出数据和图形硬件寄存器之间的映射关系. 原理:根据输入语义,图形处理器从某个寄存器取数据:然后 ...

  10. 手动为php安装memcached扩展模块

    最近公司需要新部署几台服务器,主要就是lnmp平台,这几台服务器需要部署公司的系统,由于本屌刚入职时间不长,加上又是新手,所以对公司的架构一头雾水,前前后后折腾了一个月时间,终于磕磕绊绊的将系统服务器 ...