Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5288
OO have got a array a of size n, defined a function f (l,r) represent the number of I (L<=I<=R), that there ' s no J (l& lt;=j<=r,j<>i) satisfy AI mod aj=0,now OO want to know
∑I=1->N∑J=I->NF(I,J) mod (109+< Span id= "mathjax-span-43" class= "mn" >7 ) .
Inputthere is multiple test cases. Please process till EOF.
In each test case:
First Line:an integer n (n<=10^5) indicating the size of array
Second line:contain N numbers ai (0<ai<=10000) outputfor each tests:ouput a line contain a number ans. Sample Input5 1 2 3 4 5 Sample Output23
Test instructions: To an array of size n A[i] and n each number, all f (Le,ri) and ([Le,ri] is [1,n] all the sub-range), F (Le,ri) refers to the interval [Le,ri] each satisfies certain conditions of a[i] number,
The condition is that the interval [Le,ri] does not have a J that does not equal I to make a[i]%a[j]=0, (if Le==ri, the interval has only one a[i], this is satisfied with the condition, because there is really not a J, not equal to I, then not to talk about what mode. )
Idea: Data big, violence without solution, starting from a[i]%a[j]=0, such a a[j] must be a[i] factor, that is, an interval once a[i] factor a[i] on the answer will not contribute, then for each a[i]
Extend the interval to both sides, encounter a factor closest to A[i], save position le[i],ri[i], then a[i] useful maximum interval is [le[i],ri[i]], then this how to calculate a[i] contribution to the answer, as long as in this interval included to A[i]
All the sub-ranges satisfy the conditions and contribute to the answer 1, then even if the number of intervals containing a[i], since the inclusion of a[i], then the beginning of the sub-range of Le can be taken from le[i] to I, the end point from I to ri[i], a total of (i-le[i) * (ri[i]-i);
So there is a problem, such as starting from I directly to the two sides to find the end point or will time out, need to optimize, a[i] only 10000, the number of small factor is not much, then the idea of the Sieve method to each number of its position added to the number of
A multiplier is followed to indicate that it has this factor in multiples;
1#include <cstdio>2#include <cstring>3#include <string>4#include <algorithm>5#include <Set>6#include <map>7#include <queue>8#include <vector>9#include <iterator>Ten#include <utility> One#include <sstream> A#include <iostream> -#include <cmath> -#include <stack> the using namespacestd; - Const intinf=1000000007; - Const Doubleeps=0.00000001; - Const intmaxn=1e5+5; + Const intmod=1e9+7; - typedef __int64 LL; +typedefstruct A { at intVal,pos; - }aa; - AA A[MAXN]; - intL[MAXN],R[MAXN]; -vector<int> v[10005];//The subscript in the a[] array that holds the factor for each number I - BOOLcmp (aa X,aa y) in { - if(X.val==y.val)//Note If there are the same number, precedence precedes, in order to find the nearest factor to returnx.pos<Y.pos; + returnx.val<Y.val; - } the intMain () * { $ intN;Panax Notoginseng while(~SCANF ("%d",&N)) - { thell ans=0; + for(intI=1; i<=n;i++) Ascanf"%d", &a[i].val), a[i].pos=i; the for(intI=1; i<=10005; i++) v[i].clear (); + for(intI=1; i<=n;i++) -l[i]=0, r[i]=n+1; $ //The corresponding number of the subscript is stored in the structure, and the order is to find the factor $ //the factor of each number must be less than or equal to it, the factor first appears put in front; - //when considering a number, its factors have all been found. -Sort (a,a+n,cmp); the for(intI=1; i<=n;i++) - {Wuyi inttmp=A[i].val; the for(intj=tmp;j<=10000; j+=tmp)//adding factors, not many - V[j].push_back (a[i].pos); Wu } - for(intI=1; i<=n;i++) About { $ inttmp=A[i].val; - for(intJ=v[tmp].size ()-1; j>=0; j--)//Search the location of all the factors and find the nearest - { - if(V[tmp][j]<a[i].pos) l[a[i].pos]=Max (l[a[i].pos],v[tmp][j]); A if(V[tmp][j]>a[i].pos) r[a[i].pos]=min (r[a[i].pos],v[tmp][j]); + } the } - for(intI=1; i<=n;i++) $ { the intt=A[i].pos; theAns= (ans+ (LL) (A[i].pos-l[a[i].pos]) * (r[a[i].pos]-a[i].pos))%MoD; the } theprintf"%i64d\n", ans); - } in return 0; the}
View Code
Hdu 5288 OO ' s Sequence