HDU5726 GCD
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的更多相关文章
- HDU5726 GCD(二分 + ST表)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence of N(N≤100, ...
- hdu-5726 GCD(rmq)
题目链接: GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Prob ...
- hdu5726 GCD(gcd +二分+rmq)
Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...
- HDU5726:GCD——题解
题目:hdu的5726 (我原博客的东西,正好整理过来,属于st表裸题) (可以看出我当时有多么的菜--) 这道题写了一遍,然而蒟蒻的我的时间爆炸了-- 于是看了一下学长的代码(顺便在此处%一下学长) ...
- Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用
OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...
- iOS 多线程之GCD的使用
在iOS开发中,遇到耗时操作,我们经常用到多线程技术.Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法,只需定义想要执行的任务,然后添加到适当的调度队列 ...
- 【swift】BlockOperation和GCD实用代码块
//BlockOperation // // ViewController.swift import UIKit class ViewController: UIViewController { @I ...
- 修改版: 小伙,多线程(GCD)看我就够了,骗你没好处!
多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能.具有这种能力的系 ...
- GCD的相关函数使用
GCD 是iOS多线程实现方案之一,非常常用 英文翻译过来就是伟大的中枢调度器,也有人戏称为是牛逼的中枢调度器 是苹果公司为多核的并行运算提出的解决方案 1.一次性函数 dispatch_once 顾 ...
随机推荐
- POJ 2104 K-th Number(分桶,线段树,主席树)
一道比较经典的数据结构题.可以用多种方式来做. 一,分桶法(平方分解). 根据数字x的大小和区间内不大于x的数字数量cnt的单调性,可知第k大数kth对应的cnt应该满足cnt≥k, 且kth是满足条 ...
- Mybatis- 配置
主配置文件 properties 第一种 <properties> <property name="jdbc.driver" value="com.my ...
- CentOS系统下安装Redis
1. 安装C语言环境 yum install gcc-c++ 2.下载Redis安装包 http://download.redis.io/releases/redis-3.2.9.tar.gz 3.解 ...
- 使用pip 提示UnicodeDecodeError: 'ascii' codec can't decode解决方法
python目录 Python27\Lib\site-packages 建一个文件sitecustomize.py 内容写: import sys sys.setdefaultencoding('gb ...
- python之道12
整理今天笔记,课上代码最少敲3遍. 用列表推导式做下列小题 过滤掉长度小于3的字符串列表,并将剩下的转换成大写字母 l = ['wusir', 'laonanhai', 'aa', 'b', 'tai ...
- bootstrap table加载数据
//html <table id="dailyDevTable"></table> //js $(function () { initTable(); }) ...
- javaweb基础(28)_jstl的核心标签
一.JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码 二.JSTL标签库的分类 核心 ...
- 黑马基础阶段测试题:通过字符输入流读取info.txt中的所有内容,每次读取一行,将每一行的第一个文字截取出来并打印在控制台上。
package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...
- python之斐波纳契数列
斐波纳契数列 斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,676 ...
- go包的理解
结论: import时指定的字符串,是相对于$GOPATH的目录路径,告诉了go,要去加载这个目录下所有的包文件(不包括子目录中的文件) 一个目录中所有的源文件(不包括子目录)代表了单独的一个包,这些 ...