[HDU 6069] Counting Divisors
Description
In mathematics, the function \(d(n)\) denotes the number of divisors of positive integer \(n\).
For example, \(d(12)=6\) because \(1,2,3,4,6,12\) are all \(12\)'s divisors.
In this problem, given \(l,r\) and \(k\), your task is to calculate the following thing :
Input
The first line of the input contains an integer \(T(1≤T≤15)\), denoting the number of test cases.
In each test case, there are \(3\) integers \(l,r,k~(1≤l≤r≤10^{12},r−l≤10^6,1≤k≤10^7)\).
Output
For each test case, print a single line containing an integer, denoting the answer.
Sample Input
3
1 5 1
1 10 2
1 100 3
Sample Output
10
48
2302
Source
2017 Multi-University Training Contest - Team 4
Solution
设 \(i=p_1^{a_1}p_2^{a_2}\cdots p_n^{a_n}\),有 \(d(i)=(1+a_1)(1+a_2)\cdots(1+a_n),d(i^k)=(1+k\cdot a_1)(1+k\cdot a_2)\cdots(1+k\cdot a_n)\)。
从 \(1\cdots\sqrt r\) 枚举质数,用每个质数筛 \(l\cdots r\)。
Code
#include <cstdio>
#include <cmath>
typedef long long LL;
const int N = 1000005, mod = 998244353;
int p[N], np[N], tot, f[N]; long long g[N];
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 sieve(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;
}
}
}
int main() {
sieve(1000000);
for (int T = read(); T; --T) {
LL l, r; scanf("%lld%lld", &l, &r);
int k = read(), m = sqrt(r), ans = 0;
for (int i = 1; i <= r - l + 1; ++i) f[i] = 1, g[i] = i + l - 1;
for (int i = 1; i <= tot && p[i] <= m; ++i) {
LL x = p[i] < l ? (l + p[i] - 1) / p[i] * p[i] : p[i];
for (LL j = x; j <= r; j += p[i]) {
int t = 1;
while (g[j - l + 1] % p[i] == 0) {
if ((t += k) >= mod) t -= mod;
g[j - l + 1] /= p[i];
}
f[j - l + 1] = 1LL * f[j - l + 1] * t % mod;
}
}
for (int i = 1; i <= r - l + 1; ++i) {
if (g[i] > 1) f[i] = (1LL + k) * f[i] % mod;
if ((ans += f[i]) >= mod) ans -= mod;
}
printf("%d\n", ans);
}
return 0;
}