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. 四轴飞行器飞行原理与双闭环PID控制

    四轴轴飞行器是微型飞行器的其中一种,相对于固定翼飞行器,它的方向控制灵活.抗干扰能力强.飞行稳定,能够携带一定的负载和有悬停功能,因此能够很好地进行空中拍摄.监视.侦查等功能,在军事和民用上具备广泛的 ...

  2. Support for the experimental syntax 'decorators-legacy' isn't currently enabled (7:1):

    1.产生原因:项目不支持装饰器 2.解决方法: 2.1 执行 yarn 安装完整依赖: 2.2 如果依赖时yarn.lock变化了,并且项目有git目录,则将提示的文件提交到git仓库 ? Are y ...

  3. 随笔4 Dictionary<K,V>

    本来说是想介绍一下Hashtable的,但是发现HashMap和Hashtable最开始的不同就是在于HashMap继承了AbstractMap,而Hashtable继承了Dictionary< ...

  4. sed \s

    export m1=`free|cut -d ":" -f2|sed -e "s/^\s\s*//g"|head -2|tail -1|cut -d ' ' - ...

  5. CSS3 ::before和::after伪元素的实际应用

    实例 1.清除浮动 通常我们清除清除浮动的方式就是在浮动元素后面添加一个空的Div标签,然后在设置它的清除浮动要是,使用after伪元素,我们就不需要添加无意义的div标签在html中了,下面的例子就 ...

  6. 覆盖(Override)和重写(Overload)的区别

    Overload Overload是重载的意思. 重载Overload表示同一个类中可以有多个名称相同的方法,但这些方法的参数列表各不相同(即参数个数或类型不同). Overload对我们来说可能比较 ...

  7. @ControllerAdvice全局数据预处理

    在传统项目的Controller的方法中传入参数,对象中的属性相同时,会出现以下错误         Book类和Author类     这是因为两个对象中的name相同,传参时分不清西游记和六小龄童 ...

  8. php current()函数 语法

    php current()函数 语法 作用:返回数组中的当前元素的值.直线电机工作原理 语法:current(array) 参数: 参数 描述 array 必需.规定要使用的数组. 说明:返回数组中的 ...

  9. web大文件断点续传

    1,项目调研 因为需要研究下断点上传的问题.找了很久终于找到一个比较好的项目. 在GoogleCode上面,代码弄下来超级不方便,还是配置hosts才好,把代码重新上传到了github上面. http ...

  10. Android逆向之旅---解析编译之后的Dex文件格式

    一.前言 新的一年又开始了,大家是否还记得去年年末的时候,我们还有一件事没有做,那就是解析Android中编译之后的classes.dex文件格式,我们在去年的时候已经介绍了: 如何解析编译之后的xm ...