GuGuFishtion
\[ Time Limit: 1500 ms\quad Memory Limit: 65536 kB \]
The meaning of problems
Give the definition of \ (Gu (a, b)
= \ frac {\ phi (ab)} {\ phi (a) \ phi (b)} \) obtaining \ (\ sum_ {a = 1 } ^ {m} \ sum_ {b = 1} ^ {n} Gu (a, b) (mod p) \)
Thinking
For the first Euler function, we know that the simple function is the Euler formula: \ (\ Phi (n-) n-* = (l- \ FRAC. 1 {{}} P1) * (l- \ FRAC {{P2}. 1 }) * ... * (l- \ FRAC {} {PN}. 1) \) , \ (PI \) of \ (n-\) prime factors.
For any two numbers \ (A, B \) , so \ (g = gcd (a, b) \)
- If \ (G! =. 1 \) , so \ (PI \) of \ (A \) specific quality factor, \ (Qi \) of \ (B \) specific quality factor, \ (Ti \) is \ (a, b \) common prime factors, then the \ (Gu (a, b) \) to expand, can be obtained
\ [\ begin {aligned} Gu (a, b) & = \ frac {\ phi (ab )} {\ phi (a) \ phi (b)} \\ & = \ frac {ab \ prod (1- \ frac {1} {pi}) \ prod (1- \ frac {1} {ti}) \ prod (1- \ frac {1 } {qi})} {a \ prod (1- \ frac {1} {pi}) \ prod (1- \ frac {1} {ti}) b \ prod (1 - \ frac {1} {qi }) \ prod (1- \ frac {1} {ti})} \\ & = \ frac {1} {\ prod (1- \ frac {1} {ti})} \ end {aligned} \]
now we set \ (the X-\) , \ (the X-\) includes all \ (Ti \) , then there
\ [\ Begin {aligned} Gu (a, b) & = \ frac {1} {\ prod (1- \ frac {1} {ti})} \\ & = \ frac {x} {x \ prod ( l- \ FRAC {} {Ti}. 1)} = & \\ \ FRAC {X} {\ Phi (X)} \ the aligned End {} \]
\ (X \) is also good to know how much, in fact \ ( G \) satisfied simultaneously includes all \ (Ti \) number, so we can set \ (X = G \) , can be obtained \ (Gu (a, b) = \ frac {g} {\ phi ( G)} \) . - If \ (G = 1 \) , this time there is no \ (Ti \) , but this is \ (Gu (a, b) \) after Expand all disappeared, so the answer is \ (1 \) , and \ (\ frac {1} {\ phi (1)} \) is also exactly \ (1 \) , so it can be viewed as \ (Gu (a, b) = \ frac {g} {\ phi (g)} \) .
In summary, \ (Gu (A, B) = \ FRAC {G} {\ Phi (G)} \) .
In this case we just calculate \ (gcd (a, b) = x (a \ in [1, m], b \ in [1, n]) \) the number, the answer can be calculated directly.
Here you can take advantage of Mobius classic inversion, can also take advantage of the inclusion-exclusion principle.
Order:
\ (F (i) \) represents \ (GCD \) is equal to \ (multiples of i \) logarithm
\ (G (i) \) represents \ (GCD \) is equal to \ (i \) a number of
then there is
\ [f (i) = \ lfloor \ frac {m} {i} \ rfloor \ lfloor \ frac {n} {i} \ rfloor \\ g (i) = f (i) - \ sum_ {j = 2} ^ {i * j
<= min (n, m)} g (ij) \] thus calculated backwards \ (G (I) \) , the answer will be.
Hint
emmmm, in fact, this problem often have cards, pay attention to the modulo the number of natural numbers and inverse play table position.
/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2019年08月26日 星期一 16时58分58秒
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e6 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
ll n, m;
int cas, tol, T;
int pri[maxn], phi[maxn];
bool ispri[maxn];
ll f[maxn], g[maxn], inv[maxn];
void handle() {
int mx = 1e6;
mes(ispri, 1);
tol = 0;
phi[1] = 1;
for(int i=2; i<=mx; i++) {
if(ispri[i]) {
pri[++tol] = i;
phi[i] = i-1;
}
for(int j=1; j<=tol&&i*pri[j]<=mx; j++) {
ispri[i*pri[j]] = 0;
if(i%pri[j] == 0) {
phi[i*pri[j]] = phi[i]*pri[j];
break;
} else {
phi[i*pri[j]] = phi[i]*(pri[j]-1);
}
}
}
}
int main() {
// freopen("in", "r", stdin);
handle();
inv[1] = 1;
scanf("%d", &T);
while(T--) {
ll p;
scanf("%lld%lld%lld", &n, &m, &p);
ll x = min(n, m);
for(int i=2; i<=x; i++) inv[i] = (p-p/i)*inv[p%i]%p;
for(int i=1; i<=x; i++) f[i] = (n/i)*(m/i)%p;
for(int i=x; i>=1; i--) {
g[i] = f[i];
for(int j=2; i*j<=x; j++) {
g[i] -= g[i*j];
if(g[i]<0) g[i]+=p;
}
}
ll ans = 0;
for(int i=1; i<=x; i++) {
ans += 1ll*g[i]*i%p * inv[phi[i]]%p;
ans %= p;
}
printf("%lld\n", ans);
}
return 0;
}