Gives the n number a1,a2...an, defines the function f[i]=j, (I<J), which represents the smallest J of the AJ MoD ai=0, where j is greater than I, and if no such number exists, then f[i]=0
n the number of all f[] values
First use the Sieve method O (Nlogn) to find out the approximate number of each
Then each read into a number x, first find out all the approximate, and then see if there have been any of these approximations.
This process of looking back can be maintained with an array.
Maintaining WATCH[],WATCH[AJ]=J indicates that AJ has not found a function value, and if AJ is an approximate number of x, then the function value of AJ is the position of X.
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<Set>#include<map>#include<stack>#include<vector>#include<queue>#include<string>#include<sstream>#defineEPS 1e-9#defineAll (x) X.begin (), X.end ()#defineINS (x) Inserter (X,x.begin ())#definefor (i,j,k) for (int i=j;i<=k;i++)#defineMAXN 1005#defineMAXM 40005#defineINF 0X3FFFFFFF#definePB push_back#defineMP Make_pair#defineX First#defineY Second#defineLC (K<<1)#defineRC ((k<<1) 1)using namespaceStd;typedefLong LongLL;inti,j,k,n,m,x,y,t,ans[10005],big,cas,num,len;BOOLFlag;intsum,wat[10005];vector<int> fac[10005]; voidGetfact (intsize) { for(intI=1; i<=size;i++) { for(intj=i;j<=size;j+=i) {fac[j].push_back (i); } }}intMain () {getfact (10000); while(~SCANF ("%d",&N) {memset (wat,0,sizeof(WAT)); memset (ans,0,sizeof(ans)); for(i=1; i<=n;i++) {scanf ("%d",&x); for(j=0; J<fac[x].size (); j + +) { intu=Fac[x][j]; if(Wat[u]) {k=Wat[u]; ANS[K]=i; Wat[u]=0; }} Wat[x]=i; } Sum=0; for(i=1; i<=n;i++) {sum+=Ans[i]; } printf ("%d\n", sum); } return 0;}
HDU 5211 Sieve method to find approximate