CodeChef Gcd Queries
Gcd QueriesProblem 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的更多相关文章
- Xtreme8.0 - Play with GCD dp
Play with GCD 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/play-with-g ...
- scau 2015寒假训练
并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...
- zhengrui集训笔记2
Day_6 计算几何 点积\Large 点积点积 叉积\Large 叉积叉积 极角\Large 极角极角 < π\piπ :叉积判断 else :atan2 旋转\Large 旋转旋转 左乘第一 ...
- 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] 表示第 ...
- [BZOJ 3514]Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES)
[BZOJ3514] Codechef MARCH14 GERALD07加强版 (CHEF AND GRAPH QUERIES) 题意 \(N\) 个点 \(M\) 条边的无向图,\(K\) 次询问保 ...
- Codechef Dynamic Trees and Queries
Home » Practice(Hard) » Dynamic Trees and Queries Problem Code: ANUDTQSubmit https://www.codechef.co ...
- CodeChef DGCD Dynamic GCD
CodeChef题面 Time limit 210 ms Code length Limit //内存限制也不说一下,真是的-- 50000 B OS Linux Language limit C, ...
- CodeChef DISTNUM2 Easy Queries 节点数组线段树
Description You are given an array A consisting of N positive integers. You have to answer Q queries ...
- 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- ...
随机推荐
- Linux--shell交互输入与循环语句--06
一.交互输入 1.命令用法:read a b c -> aa bb cc read命令同时可以定义多个变量值:而输入的内容默认以空格为分隔符,将值输入到对应的变量中:如果默认值输入过多,最后 ...
- python面向对象--类的内置函数
#isinstance(obj,cls)判断obj是否是类cls的实例 #issubclass(cls,cls1)判断cls是否是cls1的子类或派生类 class Foo: pass class B ...
- DispatcherServlet的工作原理
下面是DispatcherServlet的工作原理图,图片来源于网络. 下面是我从DispatcherServlet源码层面来分析其工作流程: 1.请求到达后,调用HandlerMapping来查找对 ...
- thinkphp 视图view
一. 继承Controller类 <?php namespace app\index\controller; use http\Params; use think\Config; use thi ...
- 美国知名Cloudflare网络公司遭中国顶尖黑客攻击
最近中美贸易战愈演愈烈,美国知名Cloudflare网络公司的客户的分布式拒绝服务攻击今天在恶意流量方面达到了新的高度,黑客并袭击了该公司在欧洲和美国的数据中心.根据Cloudflare首席执行官马修 ...
- Minor GC、Major GC、Full GC 区别
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11488036.html Minor GC 清理年轻代 Minor GC指新生代GC,即发生在新生代(包 ...
- LeetCode--044--通配符匹配(java)*
给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完全匹配才算 ...
- 二叉树的下一个结点(剑指offer_8)
题目描述 给定一个二叉树和其中一个结点,请找出中序遍历顺序的下一个结点并返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. public class TreeLinkNode { i ...
- 用jquery实现图片轮播
用jquery简单实现图片轮播效果,代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- P2627 修剪草坪 (单调队列优化$dp$)
题目链接 Solution 70分很简单的DP,复杂度 O(NK). 方程如下: \[f[i][1]=max(f[j][0]+sum[i]-sum[j])\]\[f[i][0]=max(f[i-1][ ...