Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5726
Give you n number, q a query, each ask you how many to L R gcd (A[l], ..., a[r]) equals gcd (A[l '],..., a[r ']).
First, using RMQ preprocessing Gcd,dp[i][j] represents the GCD from the beginning of the 2^j number.
Then use map to access the number of L r corresponding to a GCD.
We can pre-process before the inquiry, the first enumeration I, the i is the left end of the GCD (A[i],..., a[r]) The number of species will not exceed log2 (n), GCD is monotonic. (Because the GCD value changes each time, at least divided by 2)
So the number of GCD of all kinds is NLOG2 (n).
After understanding, we can enumerate I for the left end of the fixed, then two points to the right end point, calculate the number of each gcd.
Probably after understanding can knock, then is the detail question.
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <string>6#include <cstdio>7#include <vector>8#include <ctime>9#include <cmath>Ten#include <queue> One#include <Set> A#include <map> - using namespacestd; - #defineFill (x, y) memset ((x), (y), sizeof (x)) the #defineRep (i, x, y) for (int i = x; i <= y; ++i) - #defineDow (i, x, y) for (int i = x; i >= y; i.) -typedefLong LongLL; -typedef pair <int,int>P; +typedef pair <LL, ll>PLL; - ConstLL mod = 1e9 +7; + ConstLL inf =1e18; A Const intN = 1e5 +Ten; at intGCD (intAintb) { - returnB? GCD (b, a%b): A; - } - - intdp[n][ -]; -Map <int, ll>MP; in - voidST (intN) { to for(intK =1; K < +; ++k) { + for(inti =1; i + (1<< k)-1<= N; ++i) { -Dp[i][k] = GCD (Dp[i][k-1], Dp[i + (1<< (K-1))][k-1]); the } * } $ }Panax Notoginseng - intRmqintLintr) { the intK = log2 (r-l +1); + returnGCD (Dp[l][k], Dp[r-(1<< k) +1][k]); A } the + intMain () - { $ intT, N, Q, u, v; $scanf"%d", &t); - for(intCA =1; CA <= T; ++CA) { -scanf"%d", &n); the for(inti =1; I <= N; ++i) -scanf"%d", &dp[i][0]);Wuyi ST (n); the mp.clear (); - for(inti =1; I <= N; ++i) { Wu intL, r, temp = i, gcd = dp[i][0], S =i; - Do { AboutL = temp, r =N; $s = L, gcd =RMQ (i, s); - while(L <=r) { - intMid = (L + r)/2; -temp =mid; A if(RMQ (I, Mid) <gcd) { +R = Mid-1; thetemp = mid-1; - } $ Else { theL = mid +1; the } the } theMP[GCD] + = (LL) (Temp-s +1); -temp++; in} while(Temp <=n); the } theprintf"Case #%d:\n", CA); Aboutscanf"%d", &q); the while(q--) { thescanf"%d%d", &u, &v); the intGCD =rmq (U, v); +printf"%d%lld\n", GCD, MP[GCD]); - } the }Bayi return 0; the}
HDU 5726 GCD (RMQ + two)