C/C++:ios
1 #include <map> 2 #include <queue> 3 #include <cmath> 4 #include <vector> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <climits> 9 #include <iostream> 10 #include <algorithm> 11 #define INF 0x3f3f3f3f 12 using namespace std; 13 14 long long t, a, b, n; 15 16 vector <long long> v; 17 18 void get_prime() 19 { 20 v.clear(); 21 for (long long i = 2; i * i <= n; ++ i) 22 { 23 if (n % i == 0) v.push_back(i); 24 while (n % i == 0) 25 n /= i; 26 } 27 if (n > 1) v.push_back(n); 28 } 29 30 long long num(long long m) 31 { 32 long long sum = 0, top = 1, Q[1000] = {-1}; 33 for (long long i = 0; i < v.size(); ++ i) 34 { 35 long long temp = top; 36 for (long long j = 0; j < temp; ++ j) 37 Q[top ++] = -1 * v[i] * Q[j]; 38 } 39 for (long long i = 1; i < top; ++ i) 40 sum += (m / Q[i]); 41 return sum; 42 } 43 44 int main() 45 { 46 scanf("%lld", &t); 47 for (int i = 1; i <= t; ++ i) 48 { 49 scanf("%lld%lld%lld", &a, &b, &n); 50 get_prime(); 51 printf("Case #%d: %lld\n", i, b - num(b) - (a - 1 - num(a - 1))); 52 } 53 }