Gcd Queries

 
Problem code: GCDQ
 
 

All submissions for this problem are available.

Read problems statements in Mandarin Chinese and Russian.

You are given an array A of integers of size N. You will be given Q queries where each query is represented by two integers L, R. You have to find the gcd(Greatest Common Divisor) of the array after excluding the part from range L to R inclusive (1 Based indexing). You are guaranteed that after excluding the part of the array
remaining array is non empty.

Input

  • First line of input contains an integer T denoting number of test cases.
  • For each test case, first line will contain two space separated integers N, Q.
  • Next line contains N space separated integers denoting array A.
  • For next Q lines, each line will contain a query denoted by two space separated integers L, R.

Output

For each query, print a single integer representing the answer of that query.

Constraints

Subtask #1: 40 points

  • 2 ≤ T, N ≤ 100, 1 ≤ Q ≤ N, 1 ≤ A[i] ≤ 105
  • 1 ≤ L, R ≤ N and L ≤ R

Subtask #2: 60 points

  • 2 ≤ T, N ≤ 105, 1 ≤ Q ≤ N, 1 ≤ A[i] ≤ 105
  • 1 ≤ L, R ≤ N and L ≤ R
  • Sum of N over all the test cases will be less than or equal to 106.

Example

Input:
1
3 3
2 6 9
1 1
2 2
2 3 Output:
3
1
2

Explanation

For first query, the remaining part of array will be (6, 9), so answer is 3.
For second query, the remaining part of array will be (2, 9), so answer is 1.
For third query, the remaining part of array will be (2), so answer is 2.

Warning : Large IO(input output), please use faster method for IO.

求一个序列删去L~R后其余数的GCD ,,直接预处理一下前序GCD,后序GCD就OK~

#include <bits/stdc++.h>
using namespace std;
const int N = ;
int e[N],L[N],R[N],n,Q;
void Run() {
scanf("%d%d",&n,&Q);
for( int i = ; i <= n ; ++i ) scanf("%d",&e[i]);
L[] = e[]; for( int i = ; i <= n ; ++i ) L[i] = __gcd( e[i] , L[i-] ) ;
R[n] = e[n]; for( int i = n- ; i >= ; --i ) R[i] = __gcd( e[i] , R[i+] ) ;
while(Q--){
int x , y ;
scanf("%d%d",&x,&y);
if( x > ) {
if( y < n )printf("%d\n",__gcd(L[x-],R[y+]));
else printf("%d\n",L[x-]);
}
else {
if( y < n )printf("%d\n",R[y+]);
else puts("");
}
}
}
int main()
{
int _ , cas = ;
scanf("%d",&_);
while(_--)Run();
}

CodeChef Gcd Queries的更多相关文章

  1. Xtreme8.0 - Play with GCD dp

    Play with GCD 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/play-with-g ...

  2. scau 2015寒假训练

    并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...

  3. zhengrui集训笔记2

    Day_6 计算几何 点积\Large 点积点积 叉积\Large 叉积叉积 极角\Large 极角极角 < π\piπ :叉积判断 else :atan2 旋转\Large 旋转旋转 左乘第一 ...

  4. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

  5. [BZOJ 3514]Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES)

    [BZOJ3514] Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES) 题意 \(N\) 个点 \(M\) 条边的无向图,\(K\) 次询问保 ...

  6. Codechef Dynamic Trees and Queries

    Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...

  7. CodeChef DGCD Dynamic GCD

    CodeChef题面 Time limit 210 ms Code length Limit //内存限制也不说一下,真是的-- 50000 B OS Linux Language limit C, ...

  8. CodeChef DISTNUM2 Easy Queries 节点数组线段树

    Description You are given an array A consisting of N positive integers. You have to answer Q queries ...

  9. codechef Dynamic GCD [树链剖分 gcd]

    Dynamic GCD 题意:一棵树,字词树链加,树链gcd 根据\(gcd(a,b)=gcd(a,a-b)\) 得到\(gcd(a_1, a_2, ..., a_i) = gcd(a_1, a_1- ...

随机推荐

  1. Winfrom中数据的双向绑定(使用INotifyPropertyChanged)

    在WPF中新建项目是自动实现了INotifyPropertyChanged接口,用于数据绑定时非常的方便在winfrom中也可以实现INotifyPropertyChanged接口 将需要绑定的字段写 ...

  2. 全面优化MySQL(一)

    mysql执行一条查询语句的内部执行过程 权限验证 客户端通过连接器连接到 MYSQL服务器. 查询缓存 查询是否有查询缓存, 如果有缓存(之前执行过此语句),则直接返回缓存数据. 语法检查 分析器会 ...

  3. 牛客ACM赛 C 区区区间间间

    链接 C 区区区间间间 给定长度为\(n\)序列,求\[\sum_{i=1}^{n} \sum_{j=i}^{n} max-min\] 其中\(max\),\(min\)为区间最大,最小值,\(n\l ...

  4. 前端学习记录(HTML篇)

    HTML Hyper Text Markup Language,超文本标记语言,不是一种编程语言,而是一种标记语言 思想:网页中有很多数据,不同的数据可能需要不同的显示效果,一个标签相当于一个容器,想 ...

  5. Beats:如何创建一个定制的Elastic Beat

    Beats作为Elastic Stack家族中重要的部分.它可以和方便地让我们把我们的数据发送到Elasticsearch或Logstash之中.如果我们想要生成自己的Beat,请使用GitHub的b ...

  6. with上下文管理协议

    with open('data.txt', 'r') as f: lines = f.readlines() for line in lines: print(line.split()) 类上面这段代 ...

  7. FMDB复习

    //  colum/列/字段//  row/行/记录//  主键的作用是唯一标识一条记录//  sql语句注意:不区分大小写,以分号结束(不要分号也行?) //  如果增加字段,可能要指定数据类型,S ...

  8. 【Java】commons-lang3中DateUtils类方法介绍

    添加commons-lang3的Maven依赖 <dependency> <groupId>org.apache.commons</groupId> <art ...

  9. Xenu Link Sleuth 简单好用的链接测试工具

    XenuLink Sleuth 名词介绍 “Xenu链接检测侦探”是被广泛使用的死链接检测工具.可以检测到网页中的普通链接.图片.框架.插件.背景.样式表.脚本和java程序中的链接. 那么神马时候出 ...

  10. 后端技术杂谈8:OpenStack架构设计

    本文转自互联网,侵删 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutori ...