Hdu.5211.mutiple (Mathematical derivation && finding all factors of a number in a logn time period)

Source: Internet
Author: User

Mutiple accepts:476 submissions:1025Time limit:4000/2000 MS (java/others) Memory limit:65536/65536 K (java/others)Problem description
WLD has a sequencea[1..  N]    , for each1≤i<n    And he wants you to find out a minimalJ(later with the markF(i)   expression) to meetI<j≤n    MakeaJ Forai The multiple (i.e.aJ MoDai =0), if there is no suchJ, then this makesF(i)   = 0 Guaranteed1≤n≤10000,1≤ai≤10000      For any1≤i≤n    , and for any1≤i,j≤n(i! ) =J)            Meet ai  ! = aj  
Enter a description
Multiple sets of data (up toTenGroup) for each group of data: first row: one numberNRepresents the number of numbers next line: n number, in order a1,a2,... ,an          
Output description
For each set of data:  The sum of output F(i)    (for  1≤i<n )
Input sample
41 3 2 4
Output sample
6
Hint
F (1) =2f (2) =0f (3) =4f (4) =0
1#include <stdio.h>2#include <string.h>3#include <vector>4 Const intM = 1e4 +Ten ;5Std::vector <int>G[m];6 intF[m];7 intA[m];8 intN;9 Ten voidInit () One { A      for(inti =1; I <=10000; i + +) { -          for(intj = i; J <=10000; J + =i) { - G[j].push_back (i); the         } -     } - } -  + intMain () - { +    //freopen ("A.txt", "R", stdin); A init (); at      while(~ scanf ("%d", &N)) { -          for(inti =1; I <= N; i + +) scanf ("%d", &a[i]); -Memset (F,0,sizeof(f)); -         intsum =0 ; -          for(inti = n; i >0; I--) { -Sum + =F[a[i]]; in            //printf ("a[%d]=%d, F =%d\n", I, A[i], f[a[i]); -              for(intj =0; J < G[a[i]].size (); J + +) { tof[G[a[i]][j]]=i; +             } -         } theprintf ("%d\n", sum); *     } $     return 0 ;Panax Notoginseng}
View Code

Jeshen teaches the play, actually is asks 1, 2, ... the factor complexity of each number of n is reduced to O (n/1 + n/2 + n/3 ... n/n) = O (Nlogn), so the average is O (logn).

Hdu.5211.mutiple (Mathematical derivation && finding all factors of a number in a logn time period)

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.