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- ...
随机推荐
- 《图解HTTP》阅读总结
前言:<图解HTTP>是一本图文并茂的好书,讲解地很详尽.目前我只完整地读了一遍,很多地方知其然不知其所以然,接下来打算抽空再读一次.这篇博文只是做个粗略记录,第二遍读完会再来编辑细化. ...
- MySQL 简介
MySQL 简介 点击查看MySQL官方网站 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,后来被Sun公司收购,Sun公司后来又被Oracle公司收购,目前属于Oracle旗 ...
- 查询sql server占用内存的情况
se master go select * from sysperfinfo where counter_name like '%Total Server Memory (KB)%' go
- tomcat启动报错:Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
1.错误信息: 严重: Error configuring application listener of class org.springframework.web.context.ContextL ...
- fastjson合并json数组中相同的某个元素
/** * @param array JSON数组 * @param array 需合并后的某个元素名 */ private static JSONArray mgreArray(JSONArray ...
- 在MySQL登录时出现Access denied for user 'root'@'localhost' (using password: YES) 拒绝访问,并可修改MySQL密码
这两天装了一个mysql 5.7数据库,但是在本地dos登陆不上去,报错Access denied for user 'root'@'localhost'(using password:YES): 也 ...
- JDK1.8 红黑树
序言 当在10亿数据中只需要进行10几次比较就能查找到目标时,不禁感叹编程之魅力!人类之伟大呀! —— 学红黑树有感. 红黑树的应用 红黑树的应用比较广泛,主要是用它来存储有序的数据,它的时间复杂度是 ...
- sublime text 3 Package Control无法下载插件
Package Control无法下载插件,发现是因为被墙了. ubuntu18.04 下使用privoxy设置全局参见:https://www.cnblogs.com/linjunfu/p/1101 ...
- CodeForces - Path Queries (并查集+离线查询)
题目:https://vjudge.net/contest/323699#problem/A 题意:给你一棵树,然后有m个查询,每次查询问一条路径最大边小于给定查询的数量 思路:首先我们看到,我们其实 ...
- vue实现动态显示与隐藏底部导航的方法分析
本文实例讲述了vue实现动态显示与隐藏底部导航的方法.分享给大家供大家参考,具体如下: 在日常项目中,总有几个页面是要用到底部导航的,总有那么些个页面,是不需要底部导航的,这里列举一下页面底部导航的显 ...