Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

SP26045 GCDMAT2 - GCD OF MATRIX (hard)

LuoGu: SP26045 GCDMAT2 - GCD OF MATRIX (hard)

SPOJ: GCDMAT2 - GCD OF MATRIX (hard)

卡常。预处理倒数,用 double 。不容斥,直接四个数做数论分块,可以少一半的常数。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cstdio>
#include <algorithm>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 1e6 + 10, mod = 1e9 + 7;
bool vis[N];
int cnt, mx, p[N], f[N];
double w[N];
void adj (int &x) { x += x >> 31 & mod; }
void init ()
{
f[1] = 1;
vis[1] = true;
for (int i = 2; i < N; ++i)
{
if (!vis[i]) f[p[++cnt] = i] = i - 1;
for (int j = 1; j <= cnt && i * p[j] < N; ++j)
{
vis[i * p[j]] = true;
if (i % p[j] == 0)
{
f[i * p[j]] = f[i] * p[j];
break;
}
f[i * p[j]] = f[i] * f[p[j]];
}
}
for (int i = 2; i < N; ++i)
adj(f[i] += f[i - 1] - mod);
for (int i = 1; i < N; ++i) w[i] = 1.00000001 / i;
}
int get (int n, int x) { return x ? n * w[x] : mx; }
int calc (int n1, int m1, int n2, int m2)
{
int res = 0;
mx = min(n2, m2);
for (int l = 1, r; l <= mx; l = r + 1)
{
int a = get(n1, l), b = get(m1, l), c = get(n2, l), d = get(m2, l);
r = min({get(n1, a), get(m1, b), get(n2, c), get(m2, d)});
res = (res + (LL)(c - a) * (d - b) % mod * (f[r] - f[l - 1])) % mod;
}
adj(res);
return res;
}
int main ()
{
init();
int T; read(T);
scanf("%*d%*d");
for (int a, b, c, d; T; --T, puts(""))
read(a, b, c, d), write(calc(a - 1, b - 1, c, d));
return 0;
}