[HDU 5382] GCD?LCM!

Posted fly-in-milkyway

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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}^nsum_{j=1}^n[lcm(i,j)+gcd(i,j)ge n]S(n)=sum_{i=1}^nF(i) ]

Find (S(n) mod 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) mod 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] ]

[ egin{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}^{lfloorfrac{n}{d} floor}sum_{j=1}^{lfloorfrac{n}{d} floor}[i imes j imes d+d=n][gcd(i,j)=1]&=&sum_{dmid n}sum_{i=1}^{frac{n}{d}}sum_{j=1}^{frac{n}{d}}left[i imes j+1=frac{n}{d} ight][gcd(i,j)=1]&=&sum_{dmid n}sum_{i=1}^{frac{n}{d}}left[gcdleft(i,frac{frac{n}{d}-1}{i} ight)=1 ight] end{eqnarray} ]

[ g(n)=sum_{i=1}^{n+1}left[gcdleft(i,frac{n}{i} ight)=1 ight] ]

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

[ f(n)=sum_{dmid n}gleft(frac{n}{d}-1 ight)F(n)=F(n-1)+2n-1-f(n-1) ]

#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
", s[read()]);
    return 0;
}

以上是关于[HDU 5382] GCD?LCM!的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5584 LCM Walk (lcm/gcd)

HDU - 5584 LCM Walk (数论 GCD)

HDU 4497 GCD and LCM

HDU_3071 Gcd & Lcm game 素数分解 + 线段树 + 状压

hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)

HDU 4497 GCD and LCM (分解质因数)