QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#498626#9139. Prefix Divisible by Suffixucup-team004WA 439ms3868kbC++201.8kb2024-07-30 16:49:522024-07-30 16:49:52

Judging History

你现在查看的是最新测评结果

  • [2024-07-30 16:49:52]
  • 评测
  • 测评结果:WA
  • 用时:439ms
  • 内存:3868kb
  • [2024-07-30 16:49:52]
  • 提交

answer

#include <bits/stdc++.h>

using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;

i64 exgcd(i64 a, i64 b, i64 &x, i64 &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    i64 g = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
}

using i128 = __int128;

std::pair<i64, i64> merge(i64 m1, i64 a1, i64 m2, i64 a2) {
    i64 x, y;
    i64 g = exgcd(m1, m2, x, y);
    if ((a2 - a1) % g != 0) {
        return std::pair {0LL, 0LL};
    }
    i64 v = (a2 - a1) / g;
    i128 m = i128 {m1} / g * m2;
    i128 a = (i128 {x} * v % (m2 / g) * m1 + a1) % m;
    if (a < 0) {
        a += m;
    }
    m = std::min(m, i128(1E18) + 1);
    a = std::min(a, i128(1E18));
    return std::pair {m, a};
}

i64 pw10[15];
i64 dfs(i64 n, int c, int v, int len, int i, i64 m, i64 a) {
    if (a > n) {
        return 0LL;
    }
    i64 res = (n - a) / m + 1;
    if (a < pw10[len]) {
        res -= (pw10[len] - 1 - a) / m + 1;
    }
    for (int j = 1; j < i; j++) {
        int v1 = v % pw10[j];
        auto [m1, a1] = merge(m, a, (v1 + c) * pw10[j], v1);
        if (m1 == 0) {
            continue;
        }
        res -= dfs(n, c, v, len, j, m1, a1);
    }
    return res;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    pw10[0] = 1;
    for (int i = 1; i <= 14; i++) {
        pw10[i] = pw10[i - 1] * 10;
    }
    
    i64 n;
    int c;
    std::cin >> n >> c;
    
    i64 ans = 0;
    if (n == pw10[14]) {
        if (pw10[13] % c == 0) {
            ans++;
        }
    }
    
    for (int len = 1; len <= 6; len++) {
        for (int v = 0; v < pw10[len]; v++) {
            ans += dfs(n, c, v, len, len, (v + c) * pw10[len], v);
        }
    }
    
    std::cout << ans << "\n";
    
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3616kb

input:

20 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3628kb

input:

111 4

output:

9

result:

ok 1 number(s): "9"

Test #3:

score: 0
Accepted
time: 3ms
memory: 3556kb

input:

1111 10

output:

75

result:

ok 1 number(s): "75"

Test #4:

score: 0
Accepted
time: 331ms
memory: 3552kb

input:

1000000 7

output:

111529

result:

ok 1 number(s): "111529"

Test #5:

score: 0
Accepted
time: 1ms
memory: 3556kb

input:

1 1

output:

0

result:

ok 1 number(s): "0"

Test #6:

score: 0
Accepted
time: 102ms
memory: 3556kb

input:

99999 10000

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 2ms
memory: 3788kb

input:

1000 1

output:

287

result:

ok 1 number(s): "287"

Test #8:

score: 0
Accepted
time: 322ms
memory: 3660kb

input:

10000000 1

output:

3102010

result:

ok 1 number(s): "3102010"

Test #9:

score: 0
Accepted
time: 322ms
memory: 3860kb

input:

100000000 1

output:

31035571

result:

ok 1 number(s): "31035571"

Test #10:

score: 0
Accepted
time: 322ms
memory: 3560kb

input:

1000000000 1

output:

310375697

result:

ok 1 number(s): "310375697"

Test #11:

score: 0
Accepted
time: 324ms
memory: 3552kb

input:

10000000000 1

output:

3103907933

result:

ok 1 number(s): "3103907933"

Test #12:

score: 0
Accepted
time: 328ms
memory: 3620kb

input:

100000000000 1

output:

31039265058

result:

ok 1 number(s): "31039265058"

Test #13:

score: 0
Accepted
time: 340ms
memory: 3868kb

input:

1000000000000 1

output:

310394177863

result:

ok 1 number(s): "310394177863"

Test #14:

score: 0
Accepted
time: 376ms
memory: 3504kb

input:

10000000000000 1

output:

3103943538348

result:

ok 1 number(s): "3103943538348"

Test #15:

score: -100
Wrong Answer
time: 439ms
memory: 3508kb

input:

100000000000000 1

output:

31039437486029

result:

wrong answer 1st numbers differ - expected: '31039449626535', found: '31039437486029'