P4900 食堂(數學式子推導)

_lifehappy_發表於2020-09-29

P4900 食堂

推導

a n s = ∑ i = 1 n ∑ j = 1 i i j − ∑ i = 1 n ∑ j = 1 i ⌊ i j ⌋ 前 項 為 ∑ i = 1 n i ∑ j = 1 i i n v ( j ) , 兩 次 前 綴 和 即 可 得 到 後 項 為 ∑ i = 1 n ∑ j = 1 i ⌊ i j ⌋ = ∑ i = 1 n ∑ j = 1 i d ( j ) , n l o g l o g n + 2 n 即 可 得 到 ans = \sum_{i = 1} ^{n} \sum_{j = 1} ^ {i} \frac{i}{j} - \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \lfloor\frac{i}{j} \rfloor\\ 前項為\sum_{i = 1} ^{n}i \sum_{j = 1} ^{i} inv(j), 兩次字首和即可得到\\ 後項為\sum_{i = 1} ^{n} \sum_{j = 1} ^{i} \lfloor \frac{i}{j} \rfloor = \sum_{i = 1} ^{n} \sum_{j = 1} ^{i} d(j),nloglogn + 2n即可得到\\ ans=i=1nj=1ijii=1nj=1ijii=1nij=1iinv(j),i=1nj=1iji=i=1nj=1id(j),nloglogn+2n

本來想瞎開題,沒想到又碰到數學題了

程式碼

/*
  Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;

inline ll read() {
    ll f = 1, x = 0;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-')    f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f * x;
}

const int N = 1e6 + 10, mod = 998244353;

ll quick_pow(ll a, int n) {
    ll ans = 1;
    while(n) {
        if(n & 1) ans = ans * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return ans;
}

ll prime[N], inv[N], f1[N], f2[N], cnt;

bool st[N];

void init() {
    inv[1] = 1;
    for(int i = 2; i < N; i++) {
        if(!st[i]) {
            inv[i] = quick_pow(i, mod - 2);
            prime[++cnt] = i;
        }
        for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {
            st[i * prime[j]] = 1;
            inv[i * prime[j]] = inv[i] * inv[prime[j]] % mod;
            if(i % prime[j] == 0) {
                break;
            }
        }
    }
    for(int i = 1; i < N; i++) {
        inv[i] = (inv[i - 1] + inv[i]) % mod;
        for(int j = i; j <= N; j += i) {
            f2[j]++;
        }
    }
    for(int i = 1; i < N; i++) {
        f1[i] = 1ll * i * inv[i] % mod;
        f2[i] = (f2[i - 1] + f2[i]) % mod;
    }
    for(int i = 1; i < N; i++) {
        f1[i] = (f1[i - 1] + f1[i]) % mod;
        f2[i] = (f2[i - 1] + f2[i]) % mod;
    }
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    init();
    int T = read();
    while(T--) {
        int A = read(), B = read();
        printf("%lld\n", ((f1[B] - f2[B] - f1[A - 1] + f2[A - 1]) % mod + mod) % mod);
    }
	return 0;
}

相關文章