Gcd Queries(打表法)

Gcd Queries

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.
题目意思很简单也很明确就是求规定范围的数的最大公约数,于是乎我就开始入手;然而事实并非这么简单。。。。。。

#include<stdio.h>
int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
int main(){
	int a,i,j,x,y,n,m,c,f,d,e;
	scanf("%d",&a);
	for(i=0;i<a;i++){
		scanf("%d %d",&x,&y);
		int b[x+1];
		for(j=1;j<=x;j++){
			scanf("%d",&b[j]);
		}
		for(j=0;j<y;j++){
		    f=0;
			e=0;
			scanf("%d %d",&n,&m);
			for(int k=1;k<n;k++){
				if(k==1)
				c=b[k];
				c=gcd(b[k],c);
				f=1;
				}	
			for(int k=x;k>m;k--){
				if(k==x)
				d=b[k];
				d=gcd(b[k],d);
				e=1;
			}	
			if(e==1&&f==1){
				printf("%d\n",gcd(c,d));
			}
			else if(e==0&&f==1){
				printf("%d\n",c);
			}
			else if(e==1&&f==0){
				printf("%d\n",d);
			}		
		}
	}
}

在这里插入图片描述
嗯,很明确,运行超时。。。。。。
然后我便开始了优化之旅:
最终用打表的方法过了这道题。
思路:
建立两数组xx[a ]和yy[ a]分别用来存储从数组开头到末尾和从末尾到开头的前缀公因数和后缀公因数。比如;xx[3]代表的是数组前三个数的公因数,yy[3]代表的是数组后三个数的公因数。然后我们就用这两个数组对应要查找的数字再进行一次gcd就行,但遇到边界条件要进行判断一下,比如L等于数组开头和R等于数组末尾这两种情况要特判一下。
废话不多说,上代码:

#include<stdio.h>
int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
int xx[999999],yy[999999];
int main(){
	int a,i,j,x,y,n,m,c,f,d,e;
	scanf("%d",&a);
	for(i=0;i<a;i++){
		scanf("%d %d",&x,&y);
		int b[x+1];
		for(j=1;j<=x;j++){
			scanf("%d",&b[j]);
		}
		for(int k=1;k<=x;k++){          //前缀公因数打表
				if(k==1)
				xx[0]=b[k];
				xx[k]=gcd(xx[k-1],b[k]);
				}
			for(int k=x;k>=1;k--){      后缀公因数打表
				if(k==x)
				yy[k+1]=b[k];
				yy[k]=gcd(b[k],yy[k+1]);
			}	
		for(j=0;j<y;j++){
			scanf("%d %d",&n,&m);
			if(n!=1&&m!=x)
			printf("%d\n",gcd(xx[n-1],yy[m+1]));
			else if(n==1&&m!=x){
				printf("%d\n",yy[m+1]);
			}else if(n!=1&&m==x){
				printf("%d\n",xx[n-1]);
			}
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值