Problem Description
WLD likes playing with a sequencea[1.. N] . One day he's playing with a sequence ofNIntegers. For every index I, WLD wants to find the smallest indexF(i) (if exists), thatI<F(i)≤n , andaF(i) MoDai = 0. If There is no such an index F(i), we set f(i) as 0.
Input
There is multiple Cases. (at most )
For each case:
The first line contains one integers n(1≤n≤10000) .
The second line containsNIntegersa 1,a 2, . ,a N (1 ≤a i≤ 10000) , denoting the sequence WLD plays with. You can assume this all AI is distinct.
Output
For each case:
Print one integer. It denotes the sum of all F(i) for all 1≤i<n
Sample Input
41 3 2 4
Sample Output
6
hintf (1) =2 F (2) =0 F (3) =4 F (4) =0: J > I and A[j]% a[i] The minimum value of J, for all cases and the method of decomposition factor continuously maintained, reduced to n√n complexity (alt+41420 (keypad))
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;inta[10005];intb[10005];intMain () {intN; while(~SCANF ("%d",&N)) { intsum =0; Memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); for(inti =1; I <= N; i++) {scanf ("%d",&A[i]); } for(inti = n; I >=1; i--) {sum+=B[a[i]]; for(intj =1; J*j <= A[i]; J + +){ if(A[i]% J = =0) {B[j]= b[a[i]/j] =i; }}} printf ("%d\n", sum); } return 0;}View Code
Bestcoder Round #39 1002--reduction of complexity--mutiple