HUD 5726 GCD
给一个序列,多次查询区间的最大公约数,并求出同样是这个最大公约数的区间有多少个。
区间查询采用ST表,第二问查询利用区间向右延伸最大公约数递减的规律可通过二分快速找到右边界。把第一问的答案先求出来,表示要查询的公约数加入map。
然后枚举左端点二分右端点,把已经从存在map的值加上找到的区间数,不存在的就不用管了。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<list>
#include<map>
#include<math.h>
//#include<Windows.h>
using namespace std;
#define min(a,b) (a < b ? a:b)
#define max(a,b) (a > b ? a:b)
#define LL long long int
int T, N, Q;
int dp[100005][17];
int qurey[100005][3];
map<int, LL> m;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int GCD(int l, int r)
{
int flr = log2(r - l + 1), len = (1 << flr) ;
return gcd(dp[l][flr], dp[r - len + 1][flr]);
}
int main()
{
int Case = 0;
//freopen("in.txt", "r", stdin);
//freopen("out.txt", &