Pagini recente » Cod sursa (job #875736) | Cod sursa (job #2239731) | Cod sursa (job #803799) | Cod sursa (job #491707) | Cod sursa (job #1473077)
#define REP(a,b) for(int a=0; a<(b); ++a)
#define REP2(a,b) for(int a=1; a<=(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define FILL(x,y) memset(x,y,sizeof(x))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
#define x first
#define y second
#define st first
#define nd second
#define pb push_back
#define l(n) (n<<1)
#define r(n) ((n<<1)+1)
#define f(n) (n>>1)
#define lsb(a) (a&-a)
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
#ifndef ONLINE_JUDGE
#include<fstream>
ifstream cin("pinex.in");
ofstream cout("pinex.out");
#else
#include<iostream>
#endif
const int NMAX = 1000000;
const int PMAX = 1015;
const int INF = 2e31 - 1;
const long long LLINF = 2e61 - 1;
const int dx[] = {0, 0, -1, 1}; //1,1,-1,1};
const int dy[] = {-1, 1, 0, 0}; //1,-1,1,-1};
typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef pair<K, K> PKK;
typedef vector<int> VI;
LL A,B;
int p[NMAX],k;
int fact[20],f;
LL i,j;
int T;
bool viz[NMAX];
void ciur()
{
for(i=2;1LL*i*i<=B+1; ++i)
if(!viz[i])
{
p[k++]=i;
for(j=2*i; 1LL*j*j<=B; j+=i)
viz[j]=1;
}
}
int main() {
cin>>T;
while(T--)
{
cin>>A>>B;
if(1LL*p[k-1]*p[k-1]<B)
ciur();
f=0;
for(int i=0; p[i]*p[i]<=B && i<k; ++i)
{
if(B%p[i]==0)
{
fact[f++]=p[i];
while(B%p[i]==0)
B/=p[i];
}
}
if(B>1)
fact[f++]=B;
LL sol=A, lcm=1;
bool q;
for(int i=1; i<(1<<f); ++i,lcm=1,q=false){
for(int j=0; j<f; ++j)
if(i&(1<<j))
lcm*=fact[j], q=!q;
if(q)
sol-=A/lcm;
else
sol+=A/lcm;
}
cout<<sol<<"\n";
}
return 0;
}