Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
//hud 4135
#include<cstdio>
#include<iostream>
#include<vector>
#define maxn 100005
using namespace std;
vector <__int64> factor;
__int64 p[maxn];
int len_p;
void getfactor(__int64 x)
{
factor.clear();
for(__int64 i = 2; i * i <= x; i ++)
{
if(x % i == 0)
{
factor.push_back(i);
while(x % i == 0)
{
x /= i;
}
}
}
if(x > 1)
{
factor.push_back(x);
}
}
void getp()
{
p[0] = -1;
len_p = 1;
int len_factor = factor.size();
for(__int64 i = 0; i < len_factor; i ++)
{
int k = len_p;
for(__int64 j = 0; j < k; j ++)
{
p[len_p ++] = factor[i] * p[j] * (-1);
}
}
}
__int64 f(__int64 x)
{
__int64 ans = 0;
for(int i = 1; i < len_p; i ++)
{
ans += (x / p[i]);
}
return ans;
}
int main()
{
int d,cas = 1;
cin >> d;
while(d --)
{
__int64 a, b, n;
scanf("%I64d%I64d%I64d", &a, &b, &n);
getfactor(n);
printf("Case #%d: ", cas ++);
getp();
cout << b - f(b) - (a - 1 - f(a - 1)) << endl;
}
return 0;
}
/*
2
1 10 2
3 15 5
ans:5 10
*/