Hdu oj 5317 RGCDQ (2015 + School Joint Training 3rd) brute force + tips, hdu5317
Question connection: timeme
Find the largest gcd (f [I], f [j]) in a [L, R], where L <= I <j <= R, f [x] indicates the number of types of factors after I decomposition. Eg: f [10] = 2 (10 = 2*5), f [12] = 2 (12 = 2*2*3 ).
Analysis: it is easy to calculate f [x] first. Here, x is the maximum 1e6, and f [x] must be obtained within the constant time. And a little analysis will show that 1 <= f [x] <= 7, you can use a dp [I] [j] to indicate the number of j numbers from f [1] to f [I. In this way, the results can be preprocessed within the constant time, and the results can be output within the time of O (1. In addition, GCD is not used for this question. The largest f [x] In the interval is the maximum f [x] of the number of occurrences in this interval> = 2. For more information, see the code.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # define clc (a, B) memset (a, B, sizeof ()) 5 using namespace std; 6 const int M = 1e6 + 5; 7 int dp [M] [8]; 8 int f [M]; 9 10 void Pre () // preprocessing function, 11 {12 memset (dp, 0, sizeof (dp); 13 memset (f, 0, sizeof (f )); 14 for (int I = 2; I <M; I ++) // first obtain f (x, save in the f [] array 15 {16 if (f [I]) 17 continue; 18 f [I] = 1; 19 for (int j = 2; j * I <M; j ++) 20 {21 f [j * I] ++; 22} 23} 24 dp [2] [1] = 1; 25 for (int I = 3; I <M; I ++) // preprocessing dp [] array 26 {27 for (int j = 1; j <= 7; j ++) 28 {29 dp [I] [j] = dp [I-1] [j]; 30} 31 dp [I] [f [I] ++; 32} 33} 34 35 int main () 36 {37 Pre (); 38 int t; 39 scanf ("% d", & t); 40 while (t --) 41 {42 int a, B; 43 scanf ("% d", & a, & B); 44 int ret = 0; 45 for (int I = 1; I <8; I ++) 46 {// ensure I appear twice // ensure I appear 47 if (dp [B] [I]-dp [A-1] [I]> 1 & dp [B] [I]) 48 ret = max (ret, I); 49} 50 printf ("% d \ n", ret); 51}
52}