-
Notifications
You must be signed in to change notification settings - Fork 4
/
pinex.cpp
84 lines (81 loc) · 1.48 KB
/
pinex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <fstream>
#include <vector>
#define ULL unsigned long long
#define cSz 1000000
using namespace std;
ifstream in ("pinex.in"); ofstream out ("pinex.out");
bool ciur[cSz];
vector<ULL> prims;
void makeCiur();
void addMul(int i,int dpth, int k, int n,vector<int> divs);
ULL solve(ULL a, ULL b);
ULL inex(ULL a, vector<int> div);
int nrReq;
ULL a,b;
ULL suma;
int main()
{
makeCiur();
in >> nrReq;
while(nrReq--)
{
in >> a >> b;
suma = 0LL;
out << solve(a,b) << '\n';
}
}
void makeCiur()
{
for(int i = 2; i < cSz; i++)
if(!ciur[i]){prims.push_back(i);for(int j = i+i; j <cSz;j+=i)ciur[j]=true;}
}
ULL curSum=0;
ULL solve(ULL a, ULL b)
{
vector<int> bDiv;
ULL aux = b;
int i = 0;
while(aux!=1&&i<prims.size())
{
if(aux%prims[i]==0)bDiv.push_back(prims[i]);
while(aux%prims[i]==0)aux/=prims[i];
i++;
}
if(aux!=1)bDiv.push_back(aux);
return inex(a,bDiv);
}
ULL inex(ULL a, vector<int> divs)
{
int sn = 1;
int nrDivs = divs.size();
for(int i = 1; i <= nrDivs; i++)
{
curSum=0LL;
addMul(0,0,i,nrDivs,divs);
suma+=sn*curSum*1LL;
sn=(-sn);
}
return a-suma;
}
int v[1000003];
void addMul(int i,int dpth, int k, int n,vector<int> divs)
{
if(dpth==k-1)
{
for(int j = i+1; j <= n; j++)
{
v[dpth]=j;
ULL fact = 1;
for(int l = 0; l < k;l++)fact*=divs[v[l]-1];
curSum+=a/fact;
}
}
else
{
for(int j = i+1; j <=n;j++)
{
v[dpth]=j;
addMul(j,dpth+1,k,n,divs);
}
}
}