Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5288
Solution:
Define two arrays L[i],r[i], which represents the position of the number of numbers that are closest to it and the value is the a[i] factor on the left and right of the number I, then the answer that number I can contribute is (r[i]-i) * (I-l[i]), so each number x enumerates its factor y, Then we find a value of Y and the nearest x, then update the l,r array with his position. Time Complexity O (nsqrt (a)).
Code:
#include <stdio.h>#include <ctime>#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <string>#include <assert.h>#pragma COMMENT (linker, "/stack:1024000000,1024000000")using namespace STD;Const intMAXN =100010;Const intMOD =1e9+7;intNintA[MAXN];intL[MAXN], R[MAXN];intPRE[MAXN]; vector<int>G[MAXN];voidInit () { for(inti =1; I <=10000; i++) {g[i].clear (); for(intj =1; J*j <= i; J + +) {if(I%j = =0) {G[i].push_back (j);if(J*j! = i) G[i].push_back (i/j); } } }}intMain () {init (); while(~scanf("%d", &n)) { for(inti =1; I <= N; i++)scanf("%d", &a[i]);memsetL0,sizeof(L));memsetR0,sizeof(R));memset(Pre,0,sizeof(pre)); for(inti =1; I <= N; i++) {intTMP = A[i];intpos =0; for(intj =0; J < G[tmp].size (); J + +) {intt2 = G[tmp][j]; pos = max (Pre[t2],pos); } Pre[tmp] = i; L[i] = pos; } for(inti =1; I <=10000; i++) Pre[i] =100001; for(inti = n; I >=1; i--) {intTMP = A[i];intPOS = n +1; for(intj =0; J < G[tmp].size (); J + +) {intt2 = G[tmp][j]; pos = min (pre[t2], POS); } Pre[tmp] = i; R[i] = pos; }Long LongAns =0; for(inti =1; I <= N; i++) ans + = (Long Long) (R[i]-i) * (I-l[i])% MOD;printf("%lld\n", Ans%mod); }return 0;}
Copyright NOTICE: Reprint please indicate the source.
Hdu 5288 OO ' s Sequence