题目:
解题思路:
∵
(
n
−
S
(
n
)
)
m
o
d
9
=
0
(n-S(n))\ mod\ 9=0
(n−S(n)) mod 9=0
∴对于每个数n,
d
(
n
)
=
(
n
−
1
)
m
o
d
9
+
1
d(n)=(n-1)\ mod\ 9+1
d(n)=(n−1) mod 9+1
然后我们就有
若小D喜欢
A
=
x
∗
D
(
x
)
A=x*D(x)
A=x∗D(x)
则小D一定也喜欢
n
+
22680
=
(
x
+
22680
D
(
x
)
)
∗
D
(
x
)
n+22680=(x+\frac{22680}{D(x)})*D(x)
n+22680=(x+D(x)22680)∗D(x)
22680
=
l
c
m
(
1
,
2
,
3
,
…
…
,
9
,
10
)
22680=lcm(1,2,3,……,9,10)
22680=lcm(1,2,3,……,9,10)
然后我们求出22680内的小D喜欢的数然后暴力瞎搞。
Accepted code:
#include<ctime>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int C = 22680;
int T;
ll x, y, s[C+1];
inline ll read() {
ll x = 0, f = 1; char c = getchar();
while(!isdigit(c)) { if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)) x = (x<<1) + (x<<3) + c - 48,
c = getchar(); return x * f;
}
void init() {
T = read();
int F;
for (int i = 1; i <= C; i++) {
F = i * ((i-1) % 9 + 1);
if(F <= C) s[F] = 1;
}
for (int i = 1; i <= C; i++)
s[i] += s[i-1];
}
ll cal(ll x) {
return x/C * s[C] + s[x%C];
}
int main() {
init();
while(T--) {
x = read(); y = read();
printf("%lld\n", cal(y) - cal(x-1));
}
return 0;
}