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. 求助:关于shell数值比较的错误提示

    今天写了个脚本,过不了错误这一关,求大神路过瞟一眼. 1 #!/bin/bash 2 #disk use 3 disk_use() { 4 DISK_LOG=/tmp/disk_use.tmp 5 D ...

  2. python 正则表达式教程(转)

    转自:https://zhuanlan.zhihu.com/p/28920775 本节我们看一下正则表达式的相关用法,正则表达式是处理字符串的强大的工具,它有自己特定的语法结构,有了它,实现字符串的检 ...

  3. Codeforces917E

    //#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #i ...

  4. bzoj4128 Matrix 矩阵 BSGS

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4128 题解 想了十分钟没有任何思路. 然后一眼瞥见一句话"数据保证在 \(p\) 内 ...

  5. flask之创建项目

    一:安装flask workon flask_project 进入虚拟后安装flask pip install flask==0.10.1 安装指定的版本 进入虚拟环境的目录,查看创建的所有的虚拟环境 ...

  6. Centos7.5中的SElinux操作命令说明

    设置Selinux模式 setenforce 0 0表示警告模式 1表示强制模式 关闭要设置/etc/sysconfig/selinux下将"SELINUX=enforcing"改 ...

  7. 【leetcode】1078. Occurrences After Bigram

    题目如下: Given words first and second, consider occurrences in some text of the form "first second ...

  8. 【leetcode】402. Remove K Digits

    题目如下: 解题思路:我的方法是从头开始遍历num,对于任意一个num[i],在[i+1~len(num)-1]区间内找出离num[i]最近并且小于num[i]的数num[j],如果j-i <= ...

  9. 对Promise的研究4

    Promise.reject() Promise.reject(reason)方法也会返回一个新的 Promise 实例,该实例的状态为rejected. const p = Promise.reje ...

  10. SpringCLoud之搭建Zuul网关集群

    1.使用技术 Springboot,SpringCloud,Zuul,Nignx 2.目的 使用Zuul搭建微服务高可用的网关 3.项目创建 3.1 创建注册中心(略) 3.2 创建一个hello-s ...