bzoj 1101

时间:2022-04-18 21:57:02

其实这个用的是Mobius反演的第二种形式

F(d) = (n div d) * (m div d)

f(d) = [ gcd(i,j)=d ] (i in [1,a], j in [1,b])

 /**************************************************************
Problem: 1101
User: idy002
Language: C++
Result: Accepted
Time:6764 ms
Memory:1688 kb
****************************************************************/ #include <cstdio>
#include <iostream>
using namespace std; typedef long long dnt; int prm[], isnot[], mu[], ptot; void init( int n ) {
mu[] = ;
for( int i=; i<=n; i++ ) {
if( !isnot[i] ) {
prm[++ptot] = i;
mu[i] = -;
}
for( int j=; j<=ptot && i*prm[j]<=n; j++ ) {
isnot[i*prm[j]] = true;
if( i%prm[j]== ) {
mu[i*prm[j]] = ;
break;
}
mu[i*prm[j]] = -mu[i];
}
}
for( int i=; i<=n; i++ )
mu[i] += mu[i-];
}
dnt calc( int n, int m, int k ) {
dnt rt = ;
if( n>m ) swap(n,m);
n/=k;
m/=k;
for( int d=; d<=n; d++ ) {
int dd=min(n/(n/d),m/(m/d));
rt += (dnt)(n/d)*(m/d)*(mu[dd]-mu[d-]);
d=dd;
}
return rt;
}
int main() {
init();
int T;
scanf( "%d", &T );
while( T-- ) {
int n, m, k;
scanf( "%d%d%d", &n, &m, &k );
printf( "%lld\n", calc(n,m,k) );
}
}