QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#89264#5259. Skills in Pillssky3142#AC ✓55ms23552kbC++201.8kb2023-03-19 14:19:142023-03-19 14:19:17

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-19 14:19:17]
  • 评测
  • 测评结果:AC
  • 用时:55ms
  • 内存:23552kb
  • [2023-03-19 14:19:14]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using LL = long long;

LL gcd(LL a, LL b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

LL lcm(LL a, LL b)
{
    return a / gcd(a, b) * b;
}

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

int a, b;
LL x, y;
int dp_[1000009][3];

int& dp(int x, int y)
{
    return dp_[x][y + 1];
}

int f(int r, int n)
{
    auto &cur = dp(n, r);
    if (cur != -1)
        return cur;
    int nxt;
    if (r == 0)
    {
        nxt = a * b;
        if (nxt > n)
            return cur = n / a + n / b;
        n -= a * b;
        return cur = a + b + min(f(1, n), f(-1, n));
    }
    if (r == 1)
    {
        int t = (a - y) % a;
        int nxt = t * b;
        if (nxt > n)
            return cur = (n + 1) / a + n / b;
        n -= nxt;
        return cur = (nxt + 1) / a + t + min(f(1, n), f(-1, n));
    }
    else
    {
        int t = (b - x) % b;
        int nxt = t * a;
        if (nxt > n)
            return cur = (n + 1) / b + n / a;
        n -= nxt;
        return cur = (nxt + 1) / b + t + min(f(1, n), f(-1, n));
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    memset(dp_, -1, sizeof(dp_));

    int n;
    cin >> a >> b >> n;
    if (lcm(a, b) > n)
    {
        int ans = n / a + n / b;
        cout << ans << '\n';
        return 0;
    }
    LL d = gcd(a, b);
    if (d > 1)
    {
        int ans = min(n / a + (n + 1) / b, (n + 1) / a + n / b);
        cout << ans << '\n';
        return 0;
    }
    exgcd(a, b, x, y);
    cout << f(0, n) << '\n';
}

详细

Test #1:

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

input:

3 9 20

output:

8

result:

ok single line: '8'

Test #2:

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

input:

8 2 12

output:

7

result:

ok single line: '7'

Test #3:

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

input:

2 5 15

output:

10

result:

ok single line: '10'

Test #4:

score: 0
Accepted
time: 0ms
memory: 15084kb

input:

10 8 13

output:

2

result:

ok single line: '2'

Test #5:

score: 0
Accepted
time: 7ms
memory: 15240kb

input:

6 6 19

output:

6

result:

ok single line: '6'

Test #6:

score: 0
Accepted
time: 4ms
memory: 15180kb

input:

2 3 5

output:

3

result:

ok single line: '3'

Test #7:

score: 0
Accepted
time: 0ms
memory: 15212kb

input:

4 2 8

output:

6

result:

ok single line: '6'

Test #8:

score: 0
Accepted
time: 5ms
memory: 15076kb

input:

5 5 5

output:

2

result:

ok single line: '2'

Test #9:

score: 0
Accepted
time: 0ms
memory: 15100kb

input:

3 8 11

output:

4

result:

ok single line: '4'

Test #10:

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

input:

5 8 16

output:

5

result:

ok single line: '5'

Test #11:

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

input:

9 7 279

output:

70

result:

ok single line: '70'

Test #12:

score: 0
Accepted
time: 10ms
memory: 15104kb

input:

8 3 56

output:

25

result:

ok single line: '25'

Test #13:

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

input:

5 9 46

output:

14

result:

ok single line: '14'

Test #14:

score: 0
Accepted
time: 4ms
memory: 15012kb

input:

8 4 251

output:

93

result:

ok single line: '93'

Test #15:

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

input:

8 7 41

output:

10

result:

ok single line: '10'

Test #16:

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

input:

60 17 360

output:

27

result:

ok single line: '27'

Test #17:

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

input:

16 55 388

output:

31

result:

ok single line: '31'

Test #18:

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

input:

25 38 292

output:

18

result:

ok single line: '18'

Test #19:

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

input:

22 59 177

output:

11

result:

ok single line: '11'

Test #20:

score: 0
Accepted
time: 5ms
memory: 15092kb

input:

4 3 82

output:

50

result:

ok single line: '50'

Test #21:

score: 0
Accepted
time: 17ms
memory: 15132kb

input:

77 18 511543

output:

35070

result:

ok single line: '35070'

Test #22:

score: 0
Accepted
time: 55ms
memory: 15144kb

input:

37 32 987861

output:

57612

result:

ok single line: '57612'

Test #23:

score: 0
Accepted
time: 8ms
memory: 15260kb

input:

29 8 300899

output:

48059

result:

ok single line: '48059'

Test #24:

score: 0
Accepted
time: 4ms
memory: 15248kb

input:

73 83 533839

output:

13745

result:

ok single line: '13745'

Test #25:

score: 0
Accepted
time: 5ms
memory: 15112kb

input:

12 23 181193

output:

23008

result:

ok single line: '23008'

Test #26:

score: 0
Accepted
time: 5ms
memory: 15092kb

input:

2 2 864514

output:

864514

result:

ok single line: '864514'

Test #27:

score: 0
Accepted
time: 10ms
memory: 15320kb

input:

27 7 165249

output:

29765

result:

ok single line: '29765'

Test #28:

score: 0
Accepted
time: 30ms
memory: 17560kb

input:

15 2 751665

output:

429522

result:

ok single line: '429522'

Test #29:

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

input:

2 16 818146

output:

460207

result:

ok single line: '460207'

Test #30:

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

input:

43 88 631366

output:

21860

result:

ok single line: '21860'

Test #31:

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

input:

215 1222 3597

output:

18

result:

ok single line: '18'

Test #32:

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

input:

9619 3375 604892

output:

241

result:

ok single line: '241'

Test #33:

score: 0
Accepted
time: 10ms
memory: 15096kb

input:

861 1924 311511

output:

522

result:

ok single line: '522'

Test #34:

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

input:

9249 3782 866972

output:

322

result:

ok single line: '322'

Test #35:

score: 0
Accepted
time: 4ms
memory: 15012kb

input:

7055 8386 206874

output:

53

result:

ok single line: '53'

Test #36:

score: 0
Accepted
time: 4ms
memory: 15248kb

input:

6273 7732 122377

output:

34

result:

ok single line: '34'

Test #37:

score: 0
Accepted
time: 4ms
memory: 15244kb

input:

8057 7746 89137

output:

22

result:

ok single line: '22'

Test #38:

score: 0
Accepted
time: 4ms
memory: 15184kb

input:

9215 8952 74618

output:

16

result:

ok single line: '16'

Test #39:

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

input:

7246 3709 129579

output:

51

result:

ok single line: '51'

Test #40:

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

input:

4052 6785 831888

output:

327

result:

ok single line: '327'

Test #41:

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

input:

9 2 91067

output:

56916

result:

ok single line: '56916'

Test #42:

score: 0
Accepted
time: 38ms
memory: 17380kb

input:

10 3 913595

output:

406041

result:

ok single line: '406041'

Test #43:

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

input:

2 2 152575

output:

152575

result:

ok single line: '152575'

Test #44:

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

input:

2 2 927771

output:

927771

result:

ok single line: '927771'

Test #45:

score: 0
Accepted
time: 21ms
memory: 20148kb

input:

6 7 637014

output:

200204

result:

ok single line: '200204'

Test #46:

score: 0
Accepted
time: 33ms
memory: 21832kb

input:

5 3 721417

output:

400786

result:

ok single line: '400786'

Test #47:

score: 0
Accepted
time: 9ms
memory: 15280kb

input:

5 9 183079

output:

57538

result:

ok single line: '57538'

Test #48:

score: 0
Accepted
time: 28ms
memory: 18052kb

input:

9 2 579290

output:

362055

result:

ok single line: '362055'

Test #49:

score: 0
Accepted
time: 21ms
memory: 23552kb

input:

3 4 546909

output:

341817

result:

ok single line: '341817'

Test #50:

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

input:

2 2 932611

output:

932611

result:

ok single line: '932611'