Principle of Co-Prime exclusion in HDU-4135

Source: Internet
Author: User

Given a number, this question is used to determine how many digits in a certain range interact with this number.

First, we need to make it clear that if we can find the number of all numbers that are not in mutual quality in a given interval, then the number of mutual quality numbers will be obtained. This is called reverse solution.

For any positive integer N, there is a unique prime factor decomposition form. We can think that any number that does not interact with N must be a multiple of a certain prime factor, therefore, we can use a quality factor to determine the number of numbers in a range and the number of multiples of the quality factor. However, this is not a sufficient condition for all non-prime numbers. For other qualitative factors that can determine a series of numbers, what we need to do is to combine the over-capacity exclusion theorem to obtain their union.

CodeAs follows:

# Include <cstdlib> # Include <Cstring> # Include <Cstdio> # Include <Cmath> Using   Namespace  STD; typedef  Long   Long  Int  Int64; int64 a, B, num, REC [  6000000  ], Lim, ans;  Int  Idx; int64 gcd (int64 A, int64 B) {int64 T;  While  (B) {T = A; = B; B = T % B ;}  Return  A;} int64 lcm (int64 A, int64 B ){  Return A/gcd (a, B )* B ;}  Void DFS ( Int P, int64 Co, Int  Sign ){  If (P> Idx ){  Return  ;}  If (P> 0  ) {Ans + = Sign * (B/Co-(- 1 )/ CO );} For ( Int I = P + 1 ; I <= idx; ++ I) {DFS (I, lcm (CO, REC [I]), - Sign );}}  Int  Main (){  Int T, CA = 0  ; Scanf (  "  % D  " ,& T ); While (T -- ) {Idx = 0  ; Ans = 0  ; Scanf (  "  % I64d % i64d % i64d  " , & A, & B ,& Num );  If (Num = 1  ) {Printf (  " Case # % d: % i64d \ n  " , ++ Ca, B-A + 1  );  Continue  ;} Lim = ( Int ) SQRT ( Double  (Num ));  If (Num % 2 = 0  ){  While (Num %2 = 0  ) {Num >>= 1  ;} Rec [ ++ Idx] = 2  ;}  For ( Int I = 3 ; I <= lim; ++ I ){  If (Num % I = 0  ){ While (Num % I = 0  ) {Num /= I;} rec [ ++ Idx] = I ;}}  If (Num! = 1  ) {Rec [ ++ Idx] = Num;} DFS (  0 , 1 ,- 1 ); Printf (  "  Case # % d: % i64d \ n  " , ++ Ca, B-A + 1 - Ans );}  Return   0  ;} 

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.