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 ;}