Bzoj 2818 GCD

Source: Internet
Author: User

The main point of this paper is to use linear sieve method to find Euler function φ, first of all, say what is Euler function: The positive integer n, the Euler function is less than or equal to n the number of the number of n coprime. The reason to use the linear sieve is because it allows us to find the value of O (n).

First, a brief introduction to the linear sieve method to find Euler function φ: (Excerpt from Baidu)

intm[maxn],phi[maxn],p[maxn],pt;//M[i] is the minimum factor of I, P is prime, PT is a number of prime intMake () {intk,n=MAXN; phi[1]=1;  for(intI=2; i<n;i++)    {        if(!m[i])//I is the prime number{p[pt++]=m[i]=i; Phi[i]=i-1; }         for(intj=0;j<pt&& (k=p[j]*i) <n;j++) {M[k]=P[j]; if(M[i]==p[j])//in order to ensure that the future number is not re-screened, to break{Phi[k]=phi[i]*P[j];/*here Phi[k] and Phi[i] behind the ∏ (p[i]-1)/p[i] All the same (M[i]==p[j]) only one p[j], you can guarantee ∏ (p[i]-1)/p[i] in front of the same*/                 Break; }            Else{Phi[k]=phi[i]* (p[j]-1);//properties of the integrable function, F (i*k) =f (i) *f (k)            }        }    }}

After that, I'm going to go long. Find out the prefix of φ and multiply by 2 minus 1, it is good, the reduction is more than the calculation.

The code for this problem is as follows:

#include <cstdio>#include<cstdlib>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>using namespacestd; typedefLong Longll;Constll maxn=10000005; ll I_number,m;ll F[MAXN],B[MAXN],P[MAXN]; intMain () {CIN>>I_number; f[1]=1;  for(LL i=2; i<=i_number;i++)    {        if(!B[i]) {P[m++]=i; F[i]=i-1; }                  for(LL j=0; j<m&&p[j]*i<=i_number;j++) {F[p[j]*i]=f[i]*P[j]; B[P[J]*i]=1; if(! (I%p[j])) Break; F[P[J]*i]= (p[j]-1)*F[i]; }} ll I_temp=0;  for(LL i=1; i<=i_number;i++) {F[i]=f[i]+f[i-1]; }       for(LL i=1; i<=i_number;i++) {F[i]=f[i]*2-1; }       for(LL i=0; i<m;i++) {i_temp=i_temp+f[i_number/P[i]]; } cout<<i_temp<<Endl; return 0;}
Thank you for watching my blog, I hope you have some help, thank you.

Bzoj 2818 GCD

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.