Math Blind Finishing

Source: Internet
Author: User
Tags gcd

I. Prime number 1. Sieve prime number: There are two kinds of a linear sieve, a Euler sieve. The general use of Euler sieve on the line, if it is a [l,r] l R large but poor absolute value of a small interval, first with a linear sieve front, and then using Euler sieve sieve behind

  Euler sieve o (n log log n) : Note that each time I cycle starting from 2 J starting from I

1 BOOLV[n];2 intN;3 voidPrimeintN)4 {5memset (V,0,sizeof(v));6      for(intI=2; i<=n;i++)7     {8         if(V[i])Continue;9cout<<i<<" ";Ten          for(intj=1; j<=n/i;j++) v[i*j]=1; One     } A}

  linear sieve O (N): J starting from 1

1 voidPrimesintN)2 {3memset (V,0,sizeof(v));4Cnt=0;5      for(intI=2; i<=n;i++)6     {7         if(!v[i]) prime[++cnt]=i,v[i]=i;8          for(intj=1; j<=cnt;j++)9         {Ten             if(prime[j]>v[i]| | prime[j]>n/i) Break; Onev[i*prime[j]]=Prime[j]; A         } -     } -      for(intI=1; i<=cnt;i++) thecout<<prime[i]<<" "; -}
2. Mass factor decomposition: Trial Division. Combined with the 2~ sieve, scan theeach number D of the √n, if D is divisible by n, all factor d is removed from N, and the number of D that is removed is accumulated.
1 voidDivideintN)2 {3Cnt=0;4      for(intI=2; i*i<=n;i++)5     {6         if(n%i==0) 7         {8Prime[++cnt]=i; c[cnt]=0;9              while(n%i==0) n/=i,c[cnt]++;Ten         } One     } A     if(n>1) prime[++cnt]=n,c[cnt]=1; -      for(intI=1; i<=cnt;i++) -cout<<prime[i]<<"^"<<c[i]<<Endl; the}
3.example
1#include <bits/stdc++.h>2 #definell Long Long3 #defineN 100000104 #defineMoD 9982443535 using namespacestd;6 ll Prime[n],cnt,v[n];7 ll L,r,ans,ans2;8 voidPre ()9 {Ten      for(intI=2;i<1000000; i++) One     { A         if(v[i]==0) v[i]=i,prime[++cnt]=i; -          for(intj=1; j<=cnt;j++) -         { the             if(prime[j]>v[i]| | Prime[j]>1000000/i) Break; -v[i*prime[j]]=Prime[j]; -         } -     } + } - intMain () + { A pre (); atscanf"%lld%lld",&l,&R); -memset (V,0,sizeof(v)); -      for(LL i=1; i<=cnt;i++) -     { -ll St=max (1ll, (l1)/prime[i]) *prime[i]+Prime[i]; -          for(LL j=st;j<=r;j+=Prime[i]) in         { -             if(V[j-l])Continue; tov[j-l]=1; +ans++; -Ans2= (Ans2+prime[i])%MoD; the         }    *     } $printf"%lld%lld", ans,ans2);Panax Notoginseng     return 0; -}
Two. Approximate
    • Ask for approximate (set)
1. Trial Division: A slight inference: An integer n has an upper bound of approximately several numbers of 2√n2. Multiple methodinference: 1~n The sum of approximately several numbers of each number is approximately n log n
1vector<int>F[n];2 intN;3 voidPuintN)4 {5      for(intI=1; i<=n;i++)6          for(intj=1; j<=n/i;j++)7f[i*J].push_back (i);8      for(intI=1; i<=n;i++){9          for(intj=0; J<f[i].size (); j + +)Tencout<<f[i][j]<<" "; Onecout<<Endl; A     } -}
3.example Inverse prime

See P134

1#include <bits/stdc++.h>2 #defineN 20000000003 #definell Long Long4 using namespacestd;5 ll N;6 ll ans;7 intnum=1;8 inta[ One]={0,2,3,5,7, One, -, -, +, at, in};9 voidDfsintDepintDex,ll Anss,intCNT)Ten { One     if(dep==Ten) A     { -         if((anss>ans&&cnt>num) | | (anss<=ans&&cnt>=num)) -         { theans=Anss; -num=CNT; -         } -         return; +     } -     intt=1; +      for(intI=0; i<=dex;i++) A     { atDFS (dep+1, i,anss*t,cnt* (i+1)); -t*=A[DEP]; -         if(anss*t>n) Break; -     } - } - intMain () in { -scanf"%lld",&n); toDfs1, -,1,1); +printf"%lld\n", ans); -     return 0; the}
    • gcd
1. Theorem:
    • GCD (A, B) *LCM (A, b) =a*b
    • GCD (A, B) =gcd (b,a-b) =GCD (a,a-b) (a>=b)
    • GCD (A, a) =gcd (b,a mod b) (b!=0)
    2. Line GCD:
1 int gcd (int A,int  b)2{3      Return B?GCD (b,a%b): A; 4 }

Math Blind Finishing

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.