OO ' s Sequence
Topic Abstract: Gives you an integer sequence. For each of its non-empty subset intervals, how many integers are not divisible by the other number. The sum of these numbers for each non-empty set interval.
Analysis: Use L[i],r[i] to represent the nearest approximate in A[i].
Timeout code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <Set>9#include <map>Ten#include <queue> One#include <sstream> A using namespacestd; -typedefLong LongLL; - Const intINF =0x4ffffff; the Const DoubleEXP = 1e-5; - ConstLL mod = 1e9+7; - Const intms= 1e5+5; - Const intMS2 = 1e4+1; + - intA[ms]; + intL[ms]; A intR[ms]; at - intMain () - { - intN; - while(SCANF ("%d", &n)! =EOF) - { in for(inti =1; i<=n;i++) -scanf"%d",&a[i]); toa[0] =1; +a[n+1] =1; - the for(inti =1; i<=n;i++) * { $ for(intj = I1; j>=0; j--)Panax Notoginseng { - if(A[i]% a[j] = =0) the { +L[i] =J; A Break; the } + } - $ for(intj = i+1; j<=n+1; j + +) $ { - if(A[i]% a[j] = =0) - { theR[i] =J; - Break;Wuyi } the } - } WuLL ans=0; - for(inti =1; i<=n;i++) About { $Ans + = (i-l[i]) * (R[i]-i); -Ans%=MoD; - } -printf"%lld\n", ans); A } + return 0; the}
Optimization: Since each integer is not greater than 10000, we can preprocess the approximate number of each. In processing l[i] and r[i], use the pre array to represent the integers that appear earlier. See Code.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <Set>9#include <map>Ten#include <queue> One#include <sstream> A using namespacestd; -typedefLong LongLL; - Const intINF =0x4ffffff; the Const DoubleEXP = 1e-5; - ConstLL mod = 1e9+7; - Const intms= 1e5+5; - Const intMS2 = 1e4+1; + - intA[ms]; + intL[ms]; A intR[ms]; at intPre[ms]; -vector<int>Vec[ms]; - - voidInit () - { - for(inti =1; i<ms2;i++) in { - for(intj = I;j <ms2;j+=i) to { + Vec[j].push_back (i); - } the } * } $ Panax Notoginseng intMain () - { the init (); + intN; A while(SCANF ("%d", &n)! =EOF) the { + for(inti =1; i<=n;i++) -scanf"%d",&a[i]); $memset (PRE,0,sizeof(pre)); $ for(inti =1; i<=n;i++) - { - intt =0; the for(intj =0; J<vec[a[i]].size (); j + +) - {Wuyi if(Pre[vec[a[i]][j]]) thet =Max (T,pre[vec[a[i]][j]]); - } WuL[i] =T; -Pre[a[i]] =i; About } $ -memset (PRE,0,sizeof(pre)); - for(inti = n;i>0; i--) - { A intt = n+1; + for(intj =0; J<vec[a[i]].size (); j + +) the { - if(Pre[vec[a[i]][j]]) $ { thet =min (t,pre[vec[a[i]][j]]); the } the } theR[i] =T; -Pre[a[i]] =i; in } theLL ans=0; the for(inti =1; i<=n;i++) About { theAns + = (i-l[i]) * (R[i]-i); theAns%=MoD; the } +printf"%lld\n", ans); - } the return 0;Bayi}
OO ' s Sequence