QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#498634#9139. Prefix Divisible by Suffixucup-team004AC ✓4708ms3840kbC++201.9kb2024-07-30 16:56:432024-07-30 16:56:59

Judging History

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

  • [2024-07-30 16:56:59]
  • 评测
  • 测评结果:AC
  • 用时:4708ms
  • 内存:3840kb
  • [2024-07-30 16:56:43]
  • 提交

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++;
        }
        n--;
    }
    
    for (int len = 1; len <= 7; len++) {
        for (int v = len == 1 ? 0 : pw10[len - 1]; v < pw10[len]; v++) {
            ans += dfs(n, c, v, len, len, (v + c) * pw10[len], v);
        }
    }
    
    std::cout << ans << "\n";
    
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 3528kb

input:

20 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 6ms
memory: 3828kb

input:

111 4

output:

9

result:

ok 1 number(s): "9"

Test #3:

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

input:

1111 10

output:

75

result:

ok 1 number(s): "75"

Test #4:

score: 0
Accepted
time: 288ms
memory: 3608kb

input:

1000000 7

output:

111529

result:

ok 1 number(s): "111529"

Test #5:

score: 0
Accepted
time: 6ms
memory: 3840kb

input:

1 1

output:

0

result:

ok 1 number(s): "0"

Test #6:

score: 0
Accepted
time: 29ms
memory: 3592kb

input:

99999 10000

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 6ms
memory: 3540kb

input:

1000 1

output:

287

result:

ok 1 number(s): "287"

Test #8:

score: 0
Accepted
time: 3682ms
memory: 3528kb

input:

10000000 1

output:

3102010

result:

ok 1 number(s): "3102010"

Test #9:

score: 0
Accepted
time: 3675ms
memory: 3776kb

input:

100000000 1

output:

31035571

result:

ok 1 number(s): "31035571"

Test #10:

score: 0
Accepted
time: 3674ms
memory: 3588kb

input:

1000000000 1

output:

310375697

result:

ok 1 number(s): "310375697"

Test #11:

score: 0
Accepted
time: 3678ms
memory: 3480kb

input:

10000000000 1

output:

3103907933

result:

ok 1 number(s): "3103907933"

Test #12:

score: 0
Accepted
time: 3677ms
memory: 3592kb

input:

100000000000 1

output:

31039265058

result:

ok 1 number(s): "31039265058"

Test #13:

score: 0
Accepted
time: 3688ms
memory: 3828kb

input:

1000000000000 1

output:

310394177863

result:

ok 1 number(s): "310394177863"

Test #14:

score: 0
Accepted
time: 3711ms
memory: 3612kb

input:

10000000000000 1

output:

3103943538348

result:

ok 1 number(s): "3103943538348"

Test #15:

score: 0
Accepted
time: 3821ms
memory: 3524kb

input:

100000000000000 1

output:

31039449626535

result:

ok 1 number(s): "31039449626535"

Test #16:

score: 0
Accepted
time: 3970ms
memory: 3596kb

input:

100000000000000 10

output:

9041696367054

result:

ok 1 number(s): "9041696367054"

Test #17:

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

input:

100000000000000 100

output:

1744469671929

result:

ok 1 number(s): "1744469671929"

Test #18:

score: 0
Accepted
time: 4325ms
memory: 3528kb

input:

100000000000000 1000

output:

263959224215

result:

ok 1 number(s): "263959224215"

Test #19:

score: 0
Accepted
time: 4688ms
memory: 3592kb

input:

100000000000000 10000

output:

35400402243

result:

ok 1 number(s): "35400402243"

Test #20:

score: 0
Accepted
time: 4189ms
memory: 3596kb

input:

100000000000000 239

output:

870346971377

result:

ok 1 number(s): "870346971377"

Test #21:

score: 0
Accepted
time: 4115ms
memory: 3528kb

input:

99999987654321 111

output:

1606282527743

result:

ok 1 number(s): "1606282527743"

Test #22:

score: 0
Accepted
time: 4699ms
memory: 3592kb

input:

96709210826727 9568

output:

35605797003

result:

ok 1 number(s): "35605797003"

Test #23:

score: 0
Accepted
time: 4676ms
memory: 3616kb

input:

22952388910151 8412

output:

9470863043

result:

ok 1 number(s): "9470863043"

Test #24:

score: 0
Accepted
time: 4487ms
memory: 3604kb

input:

49191272026279 3065

output:

49381052989

result:

ok 1 number(s): "49381052989"

Test #25:

score: 0
Accepted
time: 4708ms
memory: 3584kb

input:

75434450109703 9205

output:

28741533023

result:

ok 1 number(s): "28741533023"

Test #26:

score: 0
Accepted
time: 4244ms
memory: 3596kb

input:

1677628193127 753

output:

5631822336

result:

ok 1 number(s): "5631822336"

Test #27:

score: 0
Accepted
time: 4648ms
memory: 3616kb

input:

1000010000 10000

output:

328824

result:

ok 1 number(s): "328824"

Extra Test:

score: 0
Extra Test Passed