题意:给定区间[a, b]和[c, d]问你有多少个不同的点对(x, y),x€[a, b],y€[c, d],gcd(x, y) == k,其中不同的意思是(5, 7)和(7, 5)是一样的。
析:一个莫比乌斯反演的入门题,G(x) = (m/i) * (n/i),但是这样是有重复的,所以我们把这个重复减去,重复的也就是给定两个区间的共同部分的数才会有重复的,所以先求出总数目,再减去共同的一半即可。
代码如下:
#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 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 = 1e5 + 10; const int maxm = 3e5 + 10; const int mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, -1, 0, 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; } bool vis[maxn]; int prime[maxn]; int mu[maxn]; void Moblus(){ mu[1] = 1; int tot = 0; for(int i = 2; i < maxn; ++i){ if(!vis[i]) prime[tot++] = i, mu[i] = -1; for(int j = 0; j < tot; ++j){ if(i * prime[j] >= maxn) break; vis[i*prime[j]] = 1; if(i % prime[j] == 0){ mu[i*prime[j]] = 0; break; } else mu[i*prime[j]] = -mu[i]; } } } int sum[maxn]; LL solve(int n, int m){ if(n > m) swap(n, m); LL ans = 0; for(int i = 1, det = 1; i <= n; i = det+1){ det = min(n/(n/i), m/(m/i)); ans += (LL)(sum[det] - sum[i-1]) * (m/i) * (n/i); } return ans; } int main(){ Moblus(); for(int i = 1; i < maxn; ++i) sum[i] = sum[i-1] + mu[i]; int T; cin >> T; for(int kase = 1; kase <= T; ++kase){ int a, b, c, d, k; scanf("%d %d %d %d %d", &a, &b, &c, &d, &k); printf("Case %d: ", kase); if(k == 0){ printf("0\n"); continue; } LL ans = solve(b/k, d/k) - solve((a-1)/k, d/k) - solve((c-1)/k, b/k) + solve((a-1)/k, (c-1)/k); int newa = max(a, c); int newb = min(b, d); if(newa <= newb) ans -= (solve(newb/k, newb/k) - solve((newa-1)/k, newb/k) - solve((newa-1)/k, newb/k) + solve((newa-1)/k, (newa-1)/k)) / 2; printf("%lld\n", ans); } return 0; }