散落星河的记忆🌠
Published on 2017-09-02 11:31 in 暂未分类 with 散落星河的记忆🌠

[HDU 5382] GCD?LCM!

Description

First we define:

(1) \(lcm(a,b)\), the least common multiple of two integers \(a\) and \(b\), is the smallest positive integer that is divisible by both \(a\) and \(b\). for example, \(lcm(2,3)=6\) and \(lcm(4,6)=12\).

(2) \(gcd(a,b)​\), the greatest common divisor of two integers \(a​\) and \(b​\), is the largest positive integer that divides both \(a​\) and \(b​\) without a remainder, \(gcd(2,3)=1​\) and \(gcd(4,6)=2​\).

(3) \([exp]\), \(exp\) is a logical expression, if the result of \(exp\) is \(true\), then \([exp]=1\), else \([exp]=0\). for example, \([1+2≥3]=1\) and \([1+2≥4]=0\).

Now Stilwell wants to calculate such a problem:

\[F(n)=\sum_{i=1}^n\sum_{j=1}^n[lcm(i,j)+gcd(i,j)\ge n]\\ S(n)=\sum_{i=1}^nF(i) \]

Find \(S(n) \bmod 258280327​\).

Input

The first line of the input contains a single number \(T​\), the number of test cases.
Next \(T​\) lines, each line contains a positive integer \(n​\).
\(T≤10^5, n≤10^6​\).

Output

\(T\) lines, find \(S(n) \bmod 258280327\).

Sample Input

8
1
2
3
4
10
100
233
11037

Sample Output

1
5
13
26
289
296582
3928449
213582482

Source

2015 Multi-University Training Contest 8

Solution

\[F(n)=n^2-\sum_{i=1}^{n}\sum_{j=1}^{n}[lcm(i,j)+gcd(i,j)<n]\\ \begin{eqnarray} F(n)-F(n-1)&=&n^2-\sum_{i=1}^{n}\sum_{j=1}^{n}[lcm(i,j)+gcd(i,j)<n]-(n-1)^2+\sum_{i=1}^{n-1}\sum_{j=1}^{n-1}[lcm(i,j)+gcd(i,j)<n-1]\\ &=&2n-1-\sum_{i=1}^{n-1}\sum_{j=1}^{n-1}[lcm(i,j)+gcd(i,j)=n-1]\\ \end{eqnarray}\\ F(n)=F(n-1)+2n-1-\sum_{i=1}^{n-1}\sum_{j=1}^{n-1}[lcm(i,j)+gcd(i,j)=n-1] \]

\[\begin{eqnarray} f(n)&=&\sum_{i=1}^{n}\sum_{j=1}^{n}[lcm(i,j)+gcd(i,j)=n]\\ &=&\sum_{d=1}^{n}\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{n}{d}\rfloor}[i\times j\times d+d=n][gcd(i,j)=1]\\ &=&\sum_{d\mid n}\sum_{i=1}^{\frac{n}{d}}\sum_{j=1}^{\frac{n}{d}}\left[i\times j+1=\frac{n}{d}\right][gcd(i,j)=1]\\ &=&\sum_{d\mid n}\sum_{i=1}^{\frac{n}{d}}\left[gcd\left(i,\frac{\frac{n}{d}-1}{i}\right)=1\right] \end{eqnarray} \]

\[g(n)=\sum_{i=1}^{n+1}\left[gcd\left(i,\frac{n}{i}\right)=1\right] \]

\(n\) 分解质因数,则一种质数要么全在 \(i\) 中,要么全在 \(\dfrac{n}{i}\) 中,因此 \(g(n)=2^m\),其中 \(m\)\(n\) 的质因数个数。

\[f(n)=\sum_{d\mid n}g\left(\frac{n}{d}-1\right)\\ F(n)=F(n-1)+2n-1-f(n-1) \]

Code

#include <cstdio>

const int N = 1000005, mod = 258280327;
int f[N], g[N], s[N], p[N], np[N], tot;

int read() {
	int x = 0; char c = getchar();
	while (c < '0' || c > '9') c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return x;
}
void build(int n) {
	for (int i = 2; i <= n; ++i) {
		if (!np[i]) p[++tot] = i;
		for (int j = 1; j <= tot && i * p[j] <= n; ++j) {
			np[i * p[j]] = 1;
			if (i % p[j] == 0) break;
		}
	}
	for (int i = 1; i <= tot; ++i)
		for (int j = p[i]; j <= n; j += p[i]) ++g[j];
	for (int i = 1; i <= n; ++i) g[i] = 1 << g[i];
	for (int i = 1; i <= n; ++i)
		for (int j = i, k = 0; j <= n; j += i, ++k)
			if ((f[j] += g[k]) >= mod) f[j] -= mod;
	for (int i = 1; i <= n; ++i) {
		s[i] = s[i - 1] + (i << 1) - 1 - f[i - 1];
		if (s[i] < 0) s[i] += mod;
		if (s[i] >= mod) s[i] -= mod;
	}
	for (int i = 2; i <= n; ++i) if ((s[i] += s[i - 1]) >= mod) s[i] -= mod;
}
int main() {
	build(1000000);
	for (int T = read(); T; --T) printf("%d\n", s[read()]);
	return 0;
}
posted @ 2019-02-09 11:12  散落星河的记忆🌠  阅读(114)  评论(0编辑  收藏  举报