Hdu 5288 OO's Sequence (2015 first case in multiple schools, 1st questions) Enumeration factor, hdu5288
Question link:Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5288
Question:In the closed interval [l, r], there is a number a [I], and a [I] cannot be the number except itself, f (l, r) it indicates the number of numbers in the range a [I]. Now we give you n numbers to calculate the sum of f (l, r) in all intervals.
Ideas:For each number a [I], locate the position L and R of the factor nearest to him on the left and right, and record it, then, each number a [I] has its L, R, and for each a [I] In f (l, r) the sum of values is (I-L + 1) * (R-I + 1)
Code:
1 # include <cstdio> 2 # include <cstdlib> 3 # include <cmath> 4 # include <cstring> 5 # include <iostream> 6 # include <queue> 7 # include <algorithm> 8 # include <vector> 9 using namespace std; 10 # define LL _ int6411 # define INF 0x3f3f3f3f12 const int MAXN = 100005; 13 # define mod 100000000714 int l [MAXN]; 15 int r [MAXN]; 16 int a [MAXN]; 17 int visl [MAXN]; 18 int visr [MAXN]; 19 LL ans; 20 int main () 21 {22 int n, I, J; 23 while (~ Scanf ("% d", & n) 24 {25 memset (visl, 0, sizeof (visl); 26 memset (visr, 0, sizeof (visr )); 27 for (I = 1; I <= n; I ++) 28 {29 scanf ("% d", & a [I]); 30 l [I] = 1; 31 r [I] = n; 32} 33 for (I = 1; I <= n; I ++) 34 {35 for (j = a [I]; j <= 10000; j + = a [I]) 36 {37 if (visr [j] & r [visr [j] = n) 38 r [visr [j] = I-1; 39} 40 visr [a [I] = I; 41} 42 for (I = n; I> 0; I --) 43 {44 for (j = a [I]; j <= 10000; j + = a [I]) 45 {46 if (visl [j] & l [visl [j] = 1) 47 l [visl [j] = I + 1; 48} 49 visl [a [I] = I; 50} 51 ans = 0; 52 for (I = 1; I <= n; I ++) 53 {54 // printf ("% d \ n", l [I], r [I]); 55 ans + = (LL) (I + 1-l [I]) * (r [I]-I + 1); 56 ans % = mod; 57} 58 printf ("% I64d \ n ", ans % mod); 59} 60 return 0; 61}View Code