"Hdoj 5288" OO ' s Sequence
Enumeration
The topic gives a function f (l,r) for the interval [l,r] How many numbers satisfy any number in the interval is not his approximate
The maximum satisfying range of the number of each position of I [1,n] is the contribution of the number of the position to the result
Two functions l[] r[] stores the number of contribution ranges for each location
Such as:
5
4 3 2 1 2 4
The five-digit contribution range is
(0,2)
(0,4)
(0,4)
(3,6)
(4,6)
Calculate Interval Range
Open an array pre[] store traversal to where each number appears before
I (1->n) the interval right boundary of the number of multiples that occur before each iteration of a number update
(r[pre[Num[i]])
I (n->1) interval left boundary of multiples of the number that occurs before each iteration of a number update
(l[pre[Num[i]])
The final result is the summation of I (1->n) (I-l[i]) * (r[i]-i) and the 10^9+7 remainder
The code is as follows
#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#define LL Long Long#define MOD 1000000007using namespace STD;intnum[100005],l[100005],r[100005],pre[100005];intMain () {intn,j,i,mm; ll ans; while(~scanf("%d", &n)) {mm = ans =0;memset(Pre,0,sizeof(pre)); for(i =1; I <= N; ++i) {scanf("%d", &num[i]); MM = Max (mm,num[i]); L[i] =0; R[i] = n+1; for(j = num[i]; j <= mm; j + = Num[i])//When a multiplier is used with a maximum variable mm to prune the speed much faster back to the left edge of the same if(Pre[j] && r[pre[j]] > i) r[pre[j]] = i; Pre[num[i]] = i; }memset(Pre,0,sizeof(pre)); MM =0; for(i = n; I >=1; -i) {mm = max (mm,num[i]); for(j = num[i]; j <= mm; j + = Num[i])if(Pre[j] && L[pre[j]] < i) l[pre[j]] = i; Pre[num[i]] = i; } for(i =1; I <= N; ++i) {ans = (ans+ ((i-l[i)) * (r[i]-i)%mod)%mod; }printf("%lld\n", ans); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Hdoj 5288" OO ' s Sequence