CodeChef Gcd Queries

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 = 100010;
int e[N],L[N],R[N],n,Q;
void Run() {
    scanf("%d%d",&n,&Q);
    for( int i = 1 ; i <= n ; ++i ) scanf("%d",&e[i]);
    L[1] = e[1]; for( int i = 2 ; i <= n ; ++i ) L[i] = __gcd( e[i] , L[i-1] ) ;
    R[n] = e[n]; for( int i = n-1 ; i >= 1 ; --i ) R[i] = __gcd( e[i] , R[i+1] ) ;
    while(Q--){
        int x , y ;
        scanf("%d%d",&x,&y);
        if( x > 1 ) {
            if( y < n )printf("%d\n",__gcd(L[x-1],R[y+1]));
            else printf("%d\n",L[x-1]);
        }
        else {
            if( y < n )printf("%d\n",R[y+1]);
            else puts("1");
        }
    }
}
int main()
{
    int _ , cas = 1 ;
    scanf("%d",&_);
    while(_--)Run();
}
View Code

 

转载于:https://www.cnblogs.com/hlmark/p/4296959.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值