【ybt高效进阶4-3-2】【luogu P1890】静态区间 / gcd区间

静态区间 / gcd区间

题目链接:ybt高效进阶4-3-2 / luogu P1890

题目大意

给你一个序列,然后每次问你一个区间的数的 gcd。

思路

很明显,你一个区间内的点重复与你现在的答案求 gcd 值是不会因此改变的。

自然想到用 ST 表的做法。

然后就好了。

代码

#include<cstdio>

using namespace std;

int n, m, a[50001], f[50001][16];
int x, y, log[50001];

void get_log() {
    
    //预处理 log
	int now = 1, x = 0;
	while ((now << 1) <= 50000) {
    
    
		for (int i = now; i < (now << 1); i++)
			log[i] = x;
		x++;
		now <<= 1;
	}
	while (now <= 50000) log[now++] = x;
}

int gcd(int x, int y) {
    
    //求 gcd
	if (!y) return x;
	return gcd(y, x % y);
}

int main() {
    
    
	get_log();
	
	scanf("%d %d", &n, &m);
	
	for (int i = 1; i <= n; i++) {
    
    
		scanf("%d", &a[i]);
		f[i][0] = a[i];
	}
	
	for (int i = 1; i <= log[n]; i++)//ST表预处理
		for (int j = 1; j <= n; j++)
			if (j + (1 << (i - 1)) > n) f[j][i] = f[j][i - 1];
				else f[j][i] = gcd(f[j][i - 1], f[j + (1 << (i - 1))][i - 1]);
	
	for (int i = 1; i <= m; i++) {
    
    
		scanf("%d %d", &x, &y);
		int size = log[y - x + 1];//ST表查询
		printf("%d\n", gcd(f[x][size], f[y - (1 << size) + 1][size]));
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43346722/article/details/115017081
今日推荐