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. 爱上MVC~一个Action多套View模版的设计

    回到目录 模块化 这个问题是在做模块化设计时出现的,在Lind.DDD.Plugins模块里,需要对应的模块实体,模块管理者,模块标识接口等,开发时,如果你的功能点属于一个模块,需要实现IPlugin ...

  2. 使用XSD校验Mybatis的SqlMapper配置文件(1)

    这篇文章以前面对SqlSessionFactoryBean的重构为基础,先简单回顾一下做了哪些操作: 新建SqlSessionFactoryBean,初始代码和mybatis-spring相同: 重构 ...

  3. DataGrid--多记录CRUD

    最近在做一个datagrid,但因为引用的Jquery,加上初学者,所以难免费尽周折.现在将完整版贴出来,跟大家分享,一起切磋,也方便自己回顾学习. ps:第一次发帖,不知排版效果如何,瑕疵勿怪. 首 ...

  4. SSIS package 更新 variable

    在Package中声明一个variable,在package运行的过程中,SSIS如何update Variable? 第一种方法:使用 Script Task 来更新Variable的值 1,创建一 ...

  5. Android线程之并发处理

    上一篇为大家介绍了关于Looper的简单知识,本篇我们介绍一下多线程的并发处理,我们知道Handler通过sendMessage()发送的消息,首先发送给了Looper,存入Looper的消息栈,之后 ...

  6. maven -- 学习笔记(一)之maven环境搭建

    首先先感谢博主的分享http://www.cnblogs.com/yjmyzz/p/3495762.html 基本概念: Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建 ...

  7. vs xamarin android StartActivity

    跳转无需intent,直接给要跳转的activity的type就可以了 StartActivity(typeof(Login));

  8. (转)Tomcat启动报Error listenerStart错误

    今天启动Tomcat启动不了,报以下错: org.apache.catalina.core.StandardContext startInternalSEVERE: Error listenerSta ...

  9. Docker - 配置DaoCloud的Docker加速器(国内registry-mirror)

    由于众所周知的原因,从Docker Hub难以高效地下载镜像. 除了使用VPN或代理之外,最为有效的方式就是使用Docker国内镜像. DaoCloud是首个提供国内免费Docker Hub镜像的团体 ...

  10. Mybatis逆向生成

    在已经有了数据库的表的时候,为了方便起见,我们可以逆向生成javabean,xml,dao接口等,当然,下载mybaits-generation的工具,我这里用的是eclipse插件,然后准备一 个x ...