The principle of tolerance and repulsion principle __

Source: Internet
Author: User
Tags greatest common divisor square root
the principle of tolerance--the idea is very important

I had to go to the hospital to check my body yesterday because I had not been kind to my body the other day (︶^︶), results The doctor said I was a mental stress caused, this let me very Meng forced, I think it is my diet before the law plus stay up late, and then strenuous exercise caused by (︶^︶), forget, today took the medicine, Feel better, and to small partners to share some small algorithm, but in this still want to remind you, the body is really important oh (^_^) ∠※, OK, let's cut to the chase, to see what bothers me a day's tolerance theorem is a ghost

definition of the principle of tolerance:
When we were in high school, we all knew this two formulas:
a∪b = a+b–a∩b (∩: overlapping portion)
a∪b∪c = a+b+c–a∩b–b∩c–c∩a +a∩b∩c

These two formulas help us solve a lot of problems that are difficult to think about with our logical ability at the time (here are some small examples):

Example 1: a final exam, a class of 15 people math score, there are 12 people with full marks, and 4 people language, the number are full marks, then the class at least a full score of the students how many people.
Analysis:
According to the word, the counted things have words, the number of full marks two categories, "the full score of mathematics" called "a Class of elements", "the full mark of the language is called" Class B element "," the language, the number are full marks "is called" is both a class B's element "," at least has a full mark schoolmate "is called" the Class A and the class B element number "the sum.
Answer:
15+12-4=23

Example 2: television station to 100 people to survey the day before the situation, 62 people have seen 2 channels, 34 people have seen 8 channels, of which 11 people have seen two channels. How many people have not seen on two channels.
100-(62+34-11) =15

Example 3:(Primary Olympiad) A school six ⑴ class has 45 students, everyone in the summer vacation to participate in the sports Training team, which participated in the football team has 25 people, joined the volleyball team has 22 people, participated in the swimming team has 24 people, the soccer, the volleyball all participates in has 12 people, the soccer, the swimming all participates has 9 people, the volleyball , swimming all participated in the 8 people, asked: Three have participated in how many people.
Analysis:
The number of people participating in the football team is 25 for the Class A element, participation in the volleyball team of 22 people as a category B elements, the number of participants in the swimming team of 24 for the C-class elements, is both a class B for soccer volleyball all participate in the 12 people, both B class and C for the football swimming all the 9 people, Both Class C and Class A are the 8 people who participate in volleyball swimming, and all three of them participate in the sum of Class A B class C which is set to X. Note: This topic says everyone participates in the sports training team, so the total number of this class is the sum of Class A B and C.
Answer:
25+22+24-12-9-8+X=45, the solution was x=3

Example 4:(high school title) in the natural numbers from 1 to 1000, how many numbers are divisible by 3 or 5. How many numbers cannot be divisible by 3 or 5.
Analysis:
Obviously, this is a repeat count problem (of course, if you are not afraid of trouble you can go to the number 3 multiples, 5 multiples). We can think of a "number divisible by 3 or 5" as a Class A and a class B element, and the number that is divisible by 3 or 5 (multiples of 15) is the number that is repeated, that is, "both A and B-class elements". The number of elements of Class A or Class B is asked. We can not calculate directly, we must first find the required conditions. 1000÷3=333 ... 1, the number that can be divisible by 3 is 333 (think, that's why.) In the same vein, other conditions can be obtained.

Example 5:(high school title) The denominator is 1001 of the most simple fraction of how many.
Analysis: This problem is actually finding the number of molecules that cannot be numerator with 1001. Because of 1001=7x11x13, it is to find the number that cannot be divisible by 7,11,13.
Answer:
In 1~1001, there are 7 multiples of 1001/7 = 143 (), 11 Multiples 1001/11 = 91 (), 13 is a multiple of 1001/13 = 77 (), and 7*11=77;77 is a multiple of 11 = 1001/77 (), and 7*13=91;91 is Multiples of 13, 1001/91 = 11 (a), with a multiple of 13 11*13=143;143 1001/143 = 7 (a). There are 1001 multiples of 1.
It is known from the principle of 1~1001 that the number that can be divisible by 7 or 11 or 13 is (143+91+77)-(13+11+7) +1=281 (), so that the number divisible by 7, 11, or 13 is 1001-281 = 720 (a). That is to say, the simplest fraction of the denominator 1001 is 720.

the two formula above is actually a formula in which the principle of tolerance is set in a very small number of cases, and if the collection is particularly numerous. Let's slowly uncover the mystery of the ⊙o⊙ principle.

The theory of tolerance and repulsion is an important combinatorial mathematical method that allows you to solve any set of size, or to compute the probability of a composite event.

The principle of tolerance and repulsion can be described as follows:
To calculate the size of several set merge sets, we will first calculate the size of all the individual collections, then subtract all two sets that intersect, then add back all three sets that intersect, subtract all four of the sets, and so on, and compute all the parts that intersect all the collections.
The form of the formula described above can be expressed as follows:

It can be written more succinctly, we use B as a collection of all AI, then the principle of tolerance becomes:

This formula is presented by de Moivre (Abraham de Moivre) .
Let's use Vienturai to represent the set a, B, and C:

Then the final area is the sum of the areas of A, B, C and minus the area of the intersection of A and B, the area between A and C, the area where B intersects with C, and the area where a, B, C intersects. Therefore, we can also solve the problem of n sets.

proof of the principle of tolerance and repulsion:

Here are some simple examples of applications:
Example 1: a simple permutation problem
The number of numbers from 0 to 9 is arranged, requiring the first number to be greater than 1, the last number being less than 8, and how many permutations there are altogether.
We can calculate its inverse problem, that is, the first element <=1 or the last element >=8.
When we set the first element <=1, we have an array of x groups, and the last element >=8 when the Y group is arranged. Then the formula can be used to solve by the principle of tolerance:
a∪b = a+b–a∩b (∩: overlapping portion)
After a simple combination of operations, we get the result:

Then by the total number of permutations 10! Minus, is the final answer.

Example 2: equation integer solution problem below I will show you in the form of code how to use the principle of tolerance in programming:

Note: Arranging combinatorial formulas in the tolerance principle is often encountered:
Example: To find the number of numbers in the specified interval with n-Reciprocity Problem Description:

Given A number N, you are asked to count the number of integers between a and B inclusive which the are relatively prime t o N. Two integers are said to is co-prime or relatively prime if they have no common positive divisors other than 1 or, equival Ently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer. Input

the contains t (0 < T <=) the number of test cases, each of the next T lines contains th Ree integers A, B, N where (1 <= a <= B <=) and (1 <=n <= 9). Output

for each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow Output format below. Sample Input

2
1 2
3 5 Sample Output

Case #1:5
Case #2:

Hint
in the ' The ' the ' five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.

This problem means I give you a number of n, and then give you a range [a,b], and then let you find out the interval of all and n coprime number of numbers (do not know what is called coprime classmate, please click the link: http://blog.csdn.net/sand8o8time/ article/details/76781541), required to enter how many sets of test data, and then input a,b,n separately, output requirements are output to the case #1: (results) of the form (๑. ๑)

For example [1,10], the number of 10− with 6 is supposed to be (10/2+10/3) but that's how much you subtract. their LCM 6 of the situation, so in addition to 10/6 is:
10− (10/2+10/3−10/6) =3
This use of the principle of tolerance, know this, the topic can be done, the first element factorization, and then binary enumeration subset, and then to the principle of tolerance (odd plus even minus).

Read my previous blog, or a certain number of the basis of the number theory may find that if the scope of the given is 1~n, then the Euler function is ' (^﹏^), good nonsense not to say, let's look at the AC code:

#include <iostream> using namespace std;
typedef long Long LL;
const int Max = 1000010;
int Primefactor[max];
int primenum; 
LL A,b,n;
    void Ergfac (int n) {primenum = 0; for (int i = 2;i*i<=n;i++) {//only need to be counted to the first square root n items, before the Euler function has been explained, please refer to my Euler function code in the comment if (n%i==0) {//Here's where the quality factor      
            The method is not very good, before the Euler function template has a better processing method primefactor[primenum++]=i;
        while (n%i==0)//The role of this cycle is to remove all the mass factors in n, such as 12 has 2 2 of the quality factor and a 3-factor n/=i;          } if (n>1) primefactor[primenum++]=n;                       Since I is looping to square root n (at first n), the value of the last N (last n,n is always changing) is likely to be greater than the square root n (at first n), and the prime number} ll solve (ll y) {ll ans=0; This ans is used to record the number of all and N coprime in the 1~y for (LL i=1;i< (1<<primenum); i++) {//i< (1<<primenum) This is not a wonderful feeling.                   I also thought for a good while to understand, this is equivalent to I<2^k, this is derived from combinatorial mathematics in the permutation of the combination (in), ck,1 ck,2 ' "" Ck,k, there is no ck,0 reason is there are k factor, at least choose one as a factor, LL ant=0;                     Ant is used to record how many of the factor LL q=1 are used; 
     Q is used to record the product of all the mass factors used   for (LL j=0;j<primenum;j++) {//j is used for bitwise operations, in the first loop we find i< (1<<primenum) if (i&
                j)) {//Look at the end of the code ant++;
            Q*=PRIMEFACTOR[J];   } if (ant&1) ans+=y/q;   
    This is the principle of the tolerance principle (odd plus even minus) else ans-=y/q;
return ans;
    int main () {int t,flag=0; 
    cin>>t;
        while (t--) {cin>>a>>b>>n;
        ERGFAC (N);
    cout<< "Case #" <<++flag<< ":" <<b-a+1-solve (B) +solve (A-1) <<endl;
return 0; This local reader can take a few sets of data to adjust their own, understand faster, this is used to judge, is mainly used to judge from the Primenum select a number of qualitative factors multiplied by q, bit operation is very easy to the whole Halo ha, but it is very flexible to use, such as the topic of the second group of test data, Primenum = 1, is 2, then I=1 can judge the i< (1<<primenum), that is, only a 1 number of cases, if Primenum = 2, then there are 1 from 2, and 2 of the case, a total of 2 of the situation  (2 out of 1 D are two types)

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.