In this case, we must first divide the two sides of the equation by K, so that the number of mutual prime numbers on both sides is the final result. The strategy we adopt is to use each number between cells to match the number of large intervals. However, if a qualitative factor of a number is decomposed every time, it will be TLE. Therefore, the quality factor of each number from 1 to is pre-processed before calculation.
The Code is as follows:
#include <cstring>#include <cstdlib>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;int a, b, c, d, k, rec[300000], idx;int start[100005], end[100005], S, E;long long int ret;void cut(int x){ int LIM = (int)sqrt(double(x)); if (x % 2 == 0) { rec[++idx] = 2; while (x % 2 == 0) { x /= 2; } } for (int i = 3; i <= LIM; i += 2) { if (x % i == 0) { rec[++idx] = i; while (x % i == 0) { x /= i; } } } if (x != 1) { rec[++idx] = x; }}void pre(){ idx = 0; for (int i = 1; i <= 100000; ++i) { start[i] = idx; cut(i); end[i] = idx; }}void dfs(int p, int num, int sign, int &sum, int l, int r){ if (p == E) { if (num > 1) { sum += sign * (r / num - (l-1) / num); } return; } dfs(p+1, num, sign, sum, l, r); dfs(p+1, num*rec[p+1], -sign, sum, l, r);}int main(){ int T, ca = 0, sum, l, r; pre(); scanf("%d", &T); while (T--) { ret = 0; scanf("%d %d %d %d %d", &a, &b, &c, &d, &k); printf( "Case %d: ", ++ca); if (!k) { puts("0"); continue; } b /= k, d /= k; if (b > d) { int t = b; b = d; d = t; } for (int i = 1; i <= b; ++i) { S = start[i], E = end[i]; sum = 0; l = i, r = d; dfs(S, 1, -1, sum, l, r); ret += (r-l+1) - sum; } printf("%I64d\n", ret); } return 0;}