QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#296168 | #7954. Special Numbers | defyers# | TL | 1ms | 3560kb | C++17 | 3.8kb | 2024-01-02 12:51:35 | 2024-01-02 12:51:35 |
Judging History
answer
/*
5 50 100
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
1
0
0
1 2 3 4 5 6 7 8 9
1 2 3
0 1 2 3 4 5 6 7 8 9
4
0 1 2 3 4 5 6 7 8
4
9
*/
#include "bits/stdc++.h"
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using ll = long long;
using pii = pair<int, int>;
int p2 = 0, p3 = 0, p5 = 0, p7 = 0;
ll k;
const ll mod = 1000000007;
ll g(int len, vector<int> &l, vector<int> &r) {
int P2 = p2 + 1;
int P3 = p3 + 1;
int P5 = p5 + 1;
int P7 = p7 + 1;
for (int i = 0; i < len; i++) if (l[i] > r[i]) return 0;
vector dp(P2, vector(P3, vector(P5, vector(P7, vector(2, 0ll)))));
dp[0][0][0][0][0] = 1;
for (int _ = 0; _ < len; _++) {
vector ndp(P2, vector(P3, vector(P5, vector(P7, vector(2, 0ll)))));
for (int c = l[_]; c <= r[_]; c++) {
// cout << c << " ";
for (int i = 0; i < P2; i++) {
for (int j = 0; j < P3; j++) {
for (int k = 0; k < P5; k++) {
for (int l = 0; l < P7; l++) {
for (int h = 0; h < 2; h++) {
if (c == 0) {
ndp[i][j][k][l][1] += dp[i][j][k][l][h];
}
if (c == 1) {
ndp[i][j][k][l][h] += dp[i][j][k][l][h];
}
if (c == 2) {
ndp[min(i + 1, p2)][j][k][l][h] += dp[i][j][k][l][h];
}
if (c == 3) {
ndp[i][min(j + 1, p3)][k][l][h] += dp[i][j][k][l][h];
}
if (c == 4) {
ndp[min(i + 2, p2)][j][k][l][h] += dp[i][j][k][l][h];
}
if (c == 5) {
ndp[i][j][min(k + 1, p5)][l][h] += dp[i][j][k][l][h];
}
if (c == 6) {
ndp[min(i + 1, p2)][min(j + 1, p3)][k][l][h] += dp[i][j][k][l][h];
}
if (c == 7) {
ndp[i][j][k][min(l + 1, p7)][h] += dp[i][j][k][l][h];
}
if (c == 8) {
ndp[min(i + 3, p2)][j][k][l][h] += dp[i][j][k][l][h];
}
if (c == 9) {
ndp[i][min(j + 2, p3)][k][l][h] += dp[i][j][k][l][h];
}
}
}
}
}
}
}
for (int i = 0; i < P2; i++) {
for (int j = 0; j < P3; j++) {
for (int k = 0; k < P5; k++) {
for (int l = 0; l < P7; l++) {
for (int h = 0; h < 2; h++) {
dp[i][j][k][l][h] = (ndp[i][j][k][l][h]) % mod;
}
}
}
}
}
// cout << endl;
}
ll ret = 0;
for (int i = 0; i < P2; i++) {
for (int j = 0; j < P3; j++) {
for (int k = 0; k < P5; k++) {
for (int l = 0; l < P7; l++) {
ret += dp[i][j][k][l][1];
}
}
}
}
if (k == 1) ret += dp[p2][p3][p5][p7][0];
return ret % mod;
}
ll f(ll n) {
if (n == 0) return 0;
ll ans = 0;
{
vector<int> L, R;
L.push_back(1), R.push_back(9);
for (ll i = 1, x = 10; n >= x; i++, x *= 10) {
ans += g(i, L, R);
L.push_back(0), R.push_back(9);
}
}
{
string s = to_string(n);
int len = s.size();
vector<int> L, R;
for (int i = 0; i < len; i++) {
L.push_back(s[i] - '0');
R.push_back(s[i] - '0');
}
for (int i = 0; i < len; i++) {
auto L2 = L, R2 = R;
if (i == 0) L2[i] = 1;
else L2[i] = 0;
R2[i]--;
for (int j = i + 1; j < len; j++) {
L2[j] = 0;
R2[j] = 9;
}
ans += g(len, L2, R2);
}
ans += g(len, L, R);
}
return ans;
}
void solve(int TC) {
ll l, r;
cin >> k >> l >> r;
while (k % 2 == 0) {
p2++;
k /= 2;
}
while (k % 3 == 0) {
p3++;
k /= 3;
}
while (k % 5 == 0) {
p5++;
k /= 5;
}
while (k % 7 == 0) {
p7++;
k /= 7;
}
ll ans = f(r) - f(l - 1);
ans = (ans % mod + mod) % mod;
cout << ans << "\n";
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cout << fixed << setprecision(10);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
solve(i);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3560kb
input:
5 1 20
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3524kb
input:
5 50 100
output:
19
result:
ok single line: '19'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3444kb
input:
15 11 19
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3500kb
input:
1 100 100000
output:
99901
result:
ok single line: '99901'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3448kb
input:
1 1 1
output:
1
result:
ok single line: '1'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3488kb
input:
10 800 43021
output:
23570
result:
ok single line: '23570'
Test #7:
score: -100
Time Limit Exceeded
input:
1125899906842624 1 100000000000000000000