P4900 食堂
推导
ans=∑i=1n∑j=1iij−∑i=1n∑j=1i⌊ij⌋前项为∑i=1ni∑j=1iinv(j),两次前缀和即可得到后项为∑i=1n∑j=1i⌊ij⌋=∑i=1n∑j=1id(j),nloglogn+2n即可得到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=1∑nj=1∑iji−i=1∑nj=1∑i⌊ji⌋前项为i=1∑nij=1∑iinv(j),两次前缀和即可得到后项为i=1∑nj=1∑i⌊ji⌋=i=1∑nj=1∑id(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 | 1using 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;
}