Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6704 Accepted Submission(s): 2653
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
Title:
Find the number of prime numbers relative to N in the range A to B
For example, the number of relative primes to 2 in 1-10 is: 1 3 5 7 9 is five
The principle of tolerance and exclusion:
When counting , care must be taken that there are no repetitions, no omissions. In order to prevent the overlapping part from being counted repeatedly, people have developed a new counting method. The basic idea of this method is to first calculate the number of all objects contained in a certain content without considering the overlapping situation, and then Then exclude the number of repeated calculations during counting , so that the results of the calculation are neither omitted nor repeated. This method of counting is called the principle of inclusion and exclusion.
for example:
If there are three types of things to be counted: A, B, and C, then the sum of the number of elements of type A, type B and type C = the number of elements of type A + the number of elements of type B + the number of elements of type C—that is, type A The number of elements of class B and the number of elements of both class A and class C—the number of elements of both class B and class C + the number of elements of both class A and class B and class C A∪ B∪C = A+B+C - A∩B - B∩C - C∩A + A∩B∩C
It is the union of A and B and C = elements in A and all elements in B and C Remove the intersection of A and B Remove the intersection of B and C Remove the intersection of C and A Add the intersection of A and B and C
We can first find the number of coprime to N in the range of 1-(A-1), then calculate the number of coprime to N in 1-B, and then use the principle of inclusion and exclusion to find A-B The number of relative primes to N in .
For example, for example 2
There are 1 and 2 in 1-2 that are relatively prime to 5, and 2
There are 12 of 1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14 that are relatively prime to 5 in 1-15
According to the principle of inclusion and exclusion 3 - the number of coprime to 5 in 15 is ( 12+2) -2 -2 + 2 is ten
Another example is the number of numbers that are relatively prime to 10 in 6-15
The numbers 1-5 that are relatively prime to 10 are: 1, 3 have two
1 - 15 and 10 coprime numbers are: 1, 3, 7, 9, 11, 13 there are 6
According to the principle of inclusion and exclusion 6 - the number of numbers that are relatively prime to 10 in 15 is (6+2)-2-2+2 is 6
But it is very troublesome to directly judge whether two numbers are coprime. Let's use another method to determine the number of coprime numbers
Take sample 2 as an example:
There is no number that is a multiple of 5 in 1-2, that is, 0
1 - 15 is a multiple of 5, there are 5, 10, 15 there are three
Then the number that is a multiple of 5 is naturally not coprime to 5, then the number coprime to 5 is
If you use the same method to find the number of numbers that are relatively prime to 10 in 6-15
We resolve 10 into the product of two prime numbers 2*5
In 1-5, there are multiples of 2: 2,4 have 2
is a multiple of 5 has : 5 a
In 1-15, there are 2, 4, 6, 8, 10, 12, 14 that are multiples of 2, and there are 7 numbers
There are 5, 10, and 15 that are multiples of 5, and there are 3 numbers
But we need to note that 10 is both a multiple of 2 and a multiple of 5, so the number 10 only needs to be subtracted once when calculating.
So according to the principle of inclusion and exclusion
The number of multiples of 2 or 5 in 15 is (3+7)-1-1+1 == (3+7)-1 == == (3+7)-10/(2*5)= =9
(The number of multiples of Y in X is X/Y, for example, the multiple of 6 in 10 is 10/6 is 1)
But according to tests, it's not all subtraction.
For example, find the multiples of 2, 3 and 5 in 1-30
It is a multiple of two: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 a total of 15
It is a multiple of three: 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 a total of 10
Is a multiple of five: 5, 10, 15, 20, 25, 30 a total of 6
Then (10+15+6)-30/(2*3)-30/(2*5)-30/(3*5)+30/(2*5*3) ==31-5-3 -2+1==22
So we see that when an odd number of primes are multiplied, they must be added. When even prime numbers are multiplied, they must be subtracted.
So how do we enumerate combinations?
like 2 and 3
3 and 5
5 and 2
2 sum 3 sum 5
Convert all numbers from 1--8 (2^ prime numbers) (excluding 8) to binary
1==> 001
2 ==> 010
3==> 011
4==> 100
5==> 101
6==> 110
7==> 111
1 means use, 0 means not use. Then 5 means use 2 and 5, 6 means use 2 and 3, 7 means use 2 and 3 and 5
This way we can nicely enumerate all combinations of prime numbers
Then we paste the code:
#include<bits/stdc++.h> using namespace std; #define LL long long #define M(a,b) memset(a,b,sizeof(a)) const int MAXN = 1e3+5; LL A,B,N; LL len; int Case; LL prime[MAXN]; void deal(LL N)///All numbers can be decomposed into the product of several prime numbers (theorem) { for(LL i =2;i*i<=N;i++){ if(N!=0 && N%i==0) prime[len++] = i; while(N!=0 && N%i==0) N/=i; } if(N>1)prime[len++]=N; } LL rc(LL num,LL len) { LL ans,temp,len2; ans=temp=len2=0; for(LL i =1;i<(1<<len);i++){///枚举范围 temp = 1;len2 = 0;///len2 is used to record the number of enumerated prime numbers for(LL j=0; j<=len; j++){ if(i & (1<<j)){len2++; temp*=prime[j];}///Enumerate prime number combinations } if(len2&1){///odd addition and even subtraction ans += num/temp; } else { ans -= num/temp; } } return ans; } intmain() { int T; cin>>T; Case=0; while(T--){ M(prime,0); len = 0; scanf("%lld %lld %lld",&A,&B,&N); deal(N); printf("Case #%d: %lld\n",Case++,(B-rc(B,len)-(A-1-rc(A-1,len)))); } return 0; }