# Principle of State compression and rejection

Source: Internet
Author: User

State compression is actually quite simple, that is, it used to open a big array when solving brute-force problems. Unfortunately, it was finally found that there was no way, and the space consumption was too large, the writing is too complicated.

After the State compression is used, it is easy to use and the space actually consumed is negligible compared with the previous array.

However, this still has some drawbacks. Because the binary storage length is limited, it does not mean that the storage size is large. There is no problem if the number of binary files is less than 20, after the quota is exceeded, you have to consider changing the method. The code of the method used is retained here.

# Define ll long

Ll getans (LL num, int m)

// State compression: calculates the number of non-reciprocal elements with N in [1, num]. m is the number of N-prime factors, all the results are calculated before calling this function.
{
Ll ans = 0, TMP, I, j, flag;

For (I = 1; I <(LL) (1 <m); I ++) // multiply by 2 ^ m first
{
TMP = 1, flag = 0;
For (j = 0; j <m; j ++)
If (I & (LL) (1 <j) // This is a bit operation process.
Flag ++, TMP * = prime [J]; // flag is used to calculate the number of duplicates, and TMP is used to reflect
If (flag % 2 = 1) // odd addition, even Subtraction
Ans + = num/tmp; // calculate the number of common factors with N
Else
Ans-= num/tmp;
}
Return ans;
}

// Here, I will write a piece of code from Baidu on the internet, share my views, and write my own code in my own book.

# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace STD;
# Define ll long
# Define maxn 70

LL Prime [maxn]; // for some relatively large arrays, they are usually opened outside the main function, so that they will not cause problems.
Ll make_ans (LL num, int m)
{
Ll ans = 0, TMP, I, j, flag;
For (I = 1; I <(LL) (1 <m); I ++) // use a binary value of 1 or 0 to indicate whether the first prime factor is used, for example, if M is 3 and the three factors are 2, 3, and 5, if I is 3, the binary value is 011, indicating that 2nd and 3 are used.
{
TMP = 1, flag = 0;
For (j = 0; j <m; j ++)
If (I & (LL) (1 <j) // determines the number of factors currently used
Flag ++, TMP * = prime [J];
If (flag & 1) // refresh principle, odd addition and even reduction
Ans + = num/tmp;
Else
Ans-= num/tmp;
}
Return ans;
}

Int main ()
{
Int T, T = 0, M;
Ll N, A, B, I;

// I do not particularly agree with this definition method, because it is all written out of the loop, although it is convenient to define the type of the variable, however, you may not be clear about its meaning when using it.
Scanf ("% d", & T );
While (t --)
{
Scanf ("% i64d % i64d % i64d", & A, & B, & N); // enter the test data
M = 0
For (I = 2; I * I <= N; I ++)

// Perform prime factor decomposition on N. xuejie says that only one solution can be missed after the opening, that is, it itself. The later if is a judgment on this situation.

If (N & N % I = 0)
{
Prime [M ++] = I;
While (N & N % I = 0)
N/= I;
} // This decomposition process is actually very simple, mainly because you write the entire process by yourself and you will find that all N's approx. (except itself) are saved in an array.
If (n> 1)
Prime [M ++] = N; // The ++ behind the array is used for counting. It just makes up for a vacancy in array usage starting from 0.
Printf ("case # % d: % i64d \ n", ++ T, (b-make_ans (B, m)-(a-1-make_ans (A-1, m )));

// Finally, the reason for Subtraction is very simple. The problem requires that the result of the solution be of mutual quality, but what we get through the function solution is the opposite result, so the result is obtained through subtraction.
}
Return 0;
}

Related Keywords:

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.