HDU 5211 Sieve method to find approximate

Source: Internet
Author: User

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&LT;&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.