Give you a sequence of N(N≤100,000)N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000)a1,...,an(0<ai≤1000,000,000). There are Q(Q≤100,000)Q(Q≤100,000) queries. For each query l,rl,r you have to calculate gcd(al,,al+1,...,ar)gcd(al,,al+1,...,ar) and count the number of pairs(l′,r′)(1≤l<r≤N)(l′,r′)(1≤l<r≤N)such that gcd(al′,al′+1,...,ar′)gcd(al′,al′+1,...,ar′)equal gcd(al,al+1,...,ar)gcd(al,al+1,...,ar).

InputThe first line of input contains a number TT, which stands for the number of test cases you need to solve.

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

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

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

For the next QQ lines, i-th line contains two number , stand for the li,rili,ri, stand for the i-th queries. 
OutputFor each case, you need to output “Case #:t” at the beginning.(with quotes, tt 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)gcd(al,al+1,...,ar) and the second number stands for the number of pairs(l′,r′)(l′,r′) such that gcd(al′,al′+1,...,ar′)gcd(al′,al′+1,...,ar′)equal gcd(al,al+1,...,ar)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很少。他们做法是二分+RMQ,我还是觉得我的做法简单

#include<bits/stdc++.h>
using namespace std;
int fun(int x,int y)
{
return __gcd(x,y);
}
#define fi first
#define se second
const int N=1e5+;
int n,a[N],l[N],v[N];
vector<pair<pair<int,int>,int> >ans[N];
unordered_map<int,long long>M;
int main()
{
ios::sync_with_stdio(),cin.tie(),cout.tie();
int T,ca=;
cin>>T;
while(T--)
{ cin>>n;
for(int i=; i<=n; i++)cin>>a[i];
for(int i=,j; i<=n; i++)
for(v[i]=a[i],j=l[i]=i; j; j=l[j]-)
{
v[j]=fun(v[j],a[i]);
while(l[j]>&&fun(a[i],v[l[j]-])==fun(a[i],v[j]))l[j]=l[l[j]-];
M[v[j]]+=j-l[j]+;
ans[i].push_back(make_pair(make_pair(j,l[j]),v[j]));
}
int q;
cin>>q;
cout<<"Case #"<<++ca<<":\n";
for(int i=,l,r,x;i<q;i++)
{
cin>>l>>r;
for(auto X:ans[r])
{
if(X.fi.fi>=l&&X.fi.se<=l)
{
x=X.se;
break;
}
}
cout<<x<<" "<<M[x]<<"\n";
}
M.clear();
for(int i=;i<=n;i++)ans[i].clear();
}
return ;
}

HDU5726 GCD的更多相关文章

  1. HDU5726 GCD(二分 + ST表)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence of N(N≤100, ...

  2. hdu-5726 GCD(rmq)

    题目链接: GCD Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Prob ...

  3. hdu5726 GCD(gcd +二分+rmq)

    Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...

  4. HDU5726:GCD——题解

    题目:hdu的5726 (我原博客的东西,正好整理过来,属于st表裸题) (可以看出我当时有多么的菜--) 这道题写了一遍,然而蒟蒻的我的时间爆炸了-- 于是看了一下学长的代码(顺便在此处%一下学长) ...

  5. Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用

    OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...

  6. iOS 多线程之GCD的使用

    在iOS开发中,遇到耗时操作,我们经常用到多线程技术.Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法,只需定义想要执行的任务,然后添加到适当的调度队列 ...

  7. 【swift】BlockOperation和GCD实用代码块

    //BlockOperation // // ViewController.swift import UIKit class ViewController: UIViewController { @I ...

  8. 修改版: 小伙,多线程(GCD)看我就够了,骗你没好处!

    多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能.具有这种能力的系 ...

  9. GCD的相关函数使用

    GCD 是iOS多线程实现方案之一,非常常用 英文翻译过来就是伟大的中枢调度器,也有人戏称为是牛逼的中枢调度器 是苹果公司为多核的并行运算提出的解决方案 1.一次性函数 dispatch_once 顾 ...

随机推荐

  1. Nginx+Keepalived双主轮询负载均衡

    双主模式使用两个VIP,前段有2台服务器,互为主从,两台服务器同时工作,不存在资源浪费情况.同时在前端的DNS服务器对网站做多条A记录,实现了Nginx的负载均衡,当一台服务器故障时候,资源会转移到另 ...

  2. 用ComboBox控件制作浏览器网址输入框

    实现效果: 知识运用: ComboBox控件的FindString public int FindString(string s) //查找数据项集合中指定数据项的索引 和Select方法 publi ...

  3. java 学习集锦

    java学习系列 http://www.cnblogs.com/skywang12345/category/455711.html

  4. SyntaxError: Non-ASCII character ‘\xe5′ in file和在代码中插入中文,python中文注释

    SyntaxError: Non-ASCII character '\xe7' in file 出现这种错误的原因是程序中的编码出问题了,只要在程序的最前面加上 #coding: utf-8 重新保存 ...

  5. C++手写快读详解(快速读入数字)

    众所周知,C++里是自带读入的(这不废话吗) 例如: int a; cin>>a; 这样的读入理解简单,适合初学者,但是非常慢. 再例如: int a; scanf("%d&qu ...

  6. CUDA开发:了解设备属性

    原文链接 今天介绍一下CUDA设备的相关属性,只有熟悉了硬件是相关属性,是怎么工作的,就能写出更适合硬件工作的代码.cudaDeviceProp这个结构体记录了设备的相关属性. struct cuda ...

  7. Bootstrap 提示工具(Tooltip)插件的事件

    事件 下表列出了提示工具(Tooltip)插件中要用到的事件.这些事件可在函数中当钩子使用. 事件 描述 实例 show.bs.tooltip 当调用 show 实例方法时立即触发该事件. $('#m ...

  8. C#继承机制 C#中的继承符合下列规则

    1.继承是可传递的.如果C从B中派生,B又从A中派生,那么C不仅继承了B中声明的成员,同样也继承了A中的成员.Object 类作为所有类的基类. 2.派生类应当是对基类的扩展.派生类可以添加新的成员, ...

  9. sql注入问题 java中将MySQL的数据库验证秘密加上 ' or '1'= '1 就可以出现万能密码

    password的字符串中,加上 ' or '1'= '1 就可以制作出万能密码. 原因如下: 原代码中密码是123456 执行数据库查询语句 实际上执行的SQL语句是: select * from ...

  10. ORTP-0.27.0移植

    注意: a. 对于0.27一下版本的ORTP的交叉编译则没有一下依赖库 b. 交叉编译工具链是: arm-linux-gnueabihf-gcc-4.9.1 (4.9版本一下的编译bctoolbox出 ...