help me in solving Puppy and GCD problem

can anyone explain this code , how this works:
code:
include <bits/stdc++.h>
using namespace std;

define N 10000007
using ll = long long;
int phi[N];
ll ac_phi[N];

void cphi(){
phi[1] = 1;
for(int i=2; i<N; i++){
if(!phi[i]){
phi[i] = i-1;
for(ll j=2i; j<N; j+=i){
if(phi[j] == 0) phi[j] = j;
phi[j] = phi[j]/i
(i-1);
}
}
}
ac_phi[0] = 0;
for(int i=1; i<N; i++) ac_phi[i] = ac_phi[i-1] + phi[i];
}

ll solve(ll n){
if(n < N) return ac_phi[n];
ll ans=(n*(n+1))/2;
for(ll k=2; k<n; ){
ll x=n/k;
ll y=n/x;
ll d=y-k+1;
ans-=solve(x)*d;
k+=d;
}
return ans;
}

int main(){
cphi();
int t; cin >> t;
while(t–){
int n, d; cin >> n >> d;
ll ans = solve(n/d);
cout << ans << ‘\n’;
}
return 0;
}