Minimum factorization and

Source: Internet
Author: User

This problem is really compared to a rowing boat on the hair.

Topic:

Given n, the sum of the minimum factorization of all composite less than or equal to n is calculated.

For 70% of data, n<=10^7.

For 100% of data, n<=10^9.

Exercises

70% Line Sieve Dafa good

100%

First we consider that for each prime number that is less than or equal to sqrt (n), and then push it a little bit, we can get a more reliable method, although the complexity of metaphysics seems to run pretty fast. Then we measure the time ... Unfortunately n=10^9 to run around 2s. (That's how I got stuck.)

The standard process is this:

We set a threshold k=100, for the quality factor of the <=k (only dozens of of them) we use the science of the capacity to engage in a, this complexity is basically not.

For >=k p We can find that n/p is within 10^7. Then in order to ensure that the factorization is the smallest, we must only select N is not a multiple of the <p prime number. Then we find that n/p obviously is not a multiple of the <p prime number.

In this way we use a violent sieve to maintain the n/p, specifically because the P increment when the n/p decrement, then we consider the upper bound of the line sieve is also decremented, each time the line sieve assignment bool array when the answer, reduce the upper bound when the number of answers deducted. Since 1kw of violent screening can be done, this is obviously scientific.

N=10^9 just run around 0.1s. In fact if the K set to 1000 run n=10^10 also just run around 0.6s.

#include <iostream>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<string.h>#include<vector>#include<math.h>#include<time.h>#include<limits>#include<Set>#include<map>using namespacestd;intSq,n;#defineFJ 100//A .#defineZS 10000005BOOLyz[zs+3];intMn=zs,cnt=0;BOOLIsPrime (intx) {     for(intp=2;p *p<=x;p++)    {        if(x%p==0)return 0; }    return 1;}intpn=0, ps[233333];Long Longans=0;voidDfsintXintLstintDEP) {    if(lst!=0) {ans+=n/x* (Long Long) ps[lst]*DEP; if(X==ps[lst]) ans-=x; }     for(inti=lst+1; i<=pn;i++)    {        if(ps[i]<=fj&& (Long Long) x*ps[i]<=n) DFS (x*ps[i],i,-DEP); Else  Break; }}voidXjintp) {     while(mn>p) cnt-=yz[mn--];}voidpjintp) {     for(intj=p;j<=mn;j+=p) {if(Yz[j])Continue; YZ[J]=1; ++CNT; }}#defineFO (x) {freopen (#x ".", "R", stdin), Freopen (#x ". Out", "w", stdout);}intMain () {//FO (Prime)scanf"%d",&N); Sq=SQRT (n) +1; intCc=0;  for(intI=2; i<=sq;i++)    {        if(IsPrime (i)) ps[++pn]=i; } DFS (1,0,-1);  for(intI=1; i<=pn;i++)    {        intCur=Ps[i]; if(cur>FJ) {XJ (n/cur); Ans+ = (n/cur-cnt-1)*(Long Long) cur;    } PJ (cur); } printf ("%lld\n", ans);}

Well, today Shin also made a flag that said he wouldn't ask for the answer to prime numbers. Then we are asking for the number of prime numbers within N.

Lost Link Run Http://mathoverflow.net/questions/81443/fastest-algorithm-to-compute-the-sum-of-primes

Minimum factorization and

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.