Common templates and conclusions in ACM number theory

Source: Internet
Author: User

1: The method of greatest common divisor

Euclidean algorithm implementation. Recursive implementation

1#include <stdio.h>2#include <string.h>3#include <algorithm>4#include <iostream>5 using namespacestd;6 __int64 gcd (__int64 y,__int64 x)7 {8__int64 ans=0;9     if(x==0)Tenans=y; One      Else AANS=GCD (x,y%x); -      returnans; - }  the intMain () - { -     intT; - __int64 A, b; +scanf"%d",&t); -scanf"%i64d%i64d",&a,&B); +         if(a>B) A Swap (A, b); atprintf"%i64d\n", gcd (A, b)); -     } -     return 0; -}

2: The relationship between least common multiple and greatest common divisor

Set N and M greatest common divisor is p, least common multiple is q, then there are the following relationships

P*q=n*m, so the greatest common divisor of two numbers can be converted into the greatest common divisor of two numbers and then the relationship between the two to seek least common multiple =

3: Determination of Prime number

1> Trial Business Law

int Is_prime () {for (int i=2; i*i<=n;i++) {    if(n%i==0  )    return0return1;}

2> Screening Method

Array a[] The number of primes within 1000000 is stored

intGet_prme () {intk=0;  for(intI=2; i<=1000000; i++)    {        if(!Chick[i]) a[k++]=i;  for(intj=0; j<k;j++)        {            if(i*a[j]>1000000)              Break; CHICK[A[J]*i]=1; if(i%a[j]==0)             Break; }    }    return 0;} //To find prime numbers by filtering method

3: Decomposition of a number of the qualitative factor, wherein a[] is a prime, f[] is a number of the mass factor = =

intGET_FX (intx) {     intj,k=0;  for(j=0; a[j]*a[j]<=x;j++)      {         if(x%a[j]==0) f[K++]=A[j];  while(x%a[j]==0) {x=x/A[j]; F[k++]=A[j]; }        if(x==1)         Break; }     if(x!=1) F[k++]=x; return 0}

4: The number that can be divisible by 11 he satisfies the absolute value of the singular number and-even number of digits and can be divisible by 11 = =

To be continued~~~

Common templates and conclusions in ACM number theory

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.