题意:给定n,m,p,问1~n,和1~m中,有多少对数满足F(gcd(i, j)) <= p,F(x) 表示 x 的质因数的个数。
析:首先要能够判断出来,如果p>=20,那么答案就是n * m,因为质因子再多,就超了5e5了,这样的话,我们就好做多了,可以用莫比乌斯反演里德优化
有了这个式子,其中F我们可以预处理出来,线性筛即可,再预处理前缀和就好,因为P<20,所以可以直接存储到数组就好了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #include <numeric> #define debug() puts("++++") #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define lowbit(x) -x&x //#define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e17; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 5e5 + 5; const int maxm = 2e4 + 10; const LL mod = 20101009; const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1}; const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } int sum[maxn][20]; int mu[maxn]; bool vis[maxn]; int prime[maxn], tot; int f[maxn]; void Moblus(){ mu[1] = 1; for(int i = 2; i < maxn; ++i){ if(!vis[i]) prime[tot++] = i, mu[i] = -1, f[i] = 1; for(int j = 0; j < tot; ++j){ int t = i * prime[j]; if(t >= maxn) break; vis[t] = 1; f[t] = f[i] + 1; if(i % prime[j] == 0) break; mu[t] = -mu[i]; } } for(int i = 1; i < maxn; ++i){ int cnt = 1; for(int j = i; j < maxn; ++cnt, j += i) sum[j][f[i]] += mu[cnt]; } FOR(i, 1, maxn) FOR(j, 1, 20) sum[i][j] += sum[i][j-1]; FOR(i, 1, maxn) FOR(j, 0, 20) sum[i][j] += sum[i-1][j]; } LL solve(int n, int m, int p){ if(n > m) swap(n, m); LL ans = 0; for(int i = 1, det; i <= n; i = det + 1){ det = min(n/(n/i), m/(m/i)); ans += (LL)(sum[det][p]-sum[i-1][p]) * (n/i) * (m/i); } return ans; } int main(){ Moblus(); int T; cin >> T; while(T--){ int p; scanf("%d %d %d", &n, &m, &p); if(p >= 20) printf("%lld\n", (LL)n * m); else printf("%lld\n", solve(n, m, p)); } return 0; }