AtCoder Beginner Contest 192 A~D Solutions

Translation Notice
This article was machine-translated using DeepSeek-R1.

  • Original Version: Authored in Chinese by myself
  • Accuracy Advisory: Potential discrepancies may exist between translations
  • Precedence: The Chinese text shall prevail in case of ambiguity
  • Feedback: Technical suggestions regarding translation quality are welcomed

A - Star

Problem Statement

What is the difference between the next multiple of 100 greater than X and X itself?
$1\le X\le 10^5$

Input Format

$X$

Output Format

Print the answer.

Sample

$X$ Output
$140$ $60$
$1000$ $100$

Analysis

The next multiple of 100 greater than $X$ is $(\lfloor X/100\rfloor+1)\times 100$. Thus, output $(\lfloor X/100\rfloor+1)\times 100-X$.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include <cstdio>
using namespace std;

int main()
{
    int x;
    scanf("%d", &x);
    printf("%d\n", (x / 100 + 1) * 100 - x);
    return 0;
}

B - uNrEaDaBlE sTrInG

Problem Statement

A string is unreadable if all characters at odd positions (1st, 3rd, 5th, etc., 1-based) are lowercase letters and all even positions are uppercase. Determine if $S$ is unreadable.

$1\le |S|\le 1000$
$S$ consists of uppercase and lowercase letters.

Input Format

$S$

Output Format

Print Yes if $S$ is unreadable, otherwise No.

Sample

$S$ Output
$\text{dIfFiCuLt}$ Yes
$\text{eASY}$ No
$\text{a}$ Yes

Analysis

Check each position according to the rules.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdio>
using namespace std;

int main()
{
    char c;
    int n = 0;
    while((c = getchar()) != '\n')
    {
        if(n++ % 2 == 0)
        {
            if(c < 'a' || c > 'z')
            {
                puts("No");
                return 0;
            }
            continue;
        }
        if(c < 'A' || c > 'Z')
        {
            puts("No");
            return 0;
        }
    }
    puts("Yes");
    return 0;
}

C - Kaprekar Number

Problem Statement

For natural number $x$, define:

  • $g1(x)$: $x$’s digits sorted in descending order
  • $g2(x)$: $x$’s digits sorted in ascending order (leading zeros removed)
  • $f(x) = g1(x) - g2(x)$

Example: $g1(314)=431$, $g2(3021)=123$, $f(271)=594$.
Given $N$ and $K$, perform $K$ iterations of $N := f(N)$ and output the final $N$.

$0\le N\le 10^9$
$1\le K\le 10^5$

Input Format

$N~K$

Output Format

Print the final $N$.

Sample

$N$ $K$ Output
$314$ $2$ $693$
$1000000000$ $100$ $0$
$6174$ $100000$ $6174$

Analysis

Use bucket sort to compute $f(n)$ efficiently, achieving $\mathcal O(K)$ total complexity.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cstdio>
#include <set>
#include <vector>
using namespace std;

int cnt[10];

int f(int x)
{
    for(int i=0; i<10; i++) cnt[i] = 0;
    while(x > 0)
    {
        cnt[x % 10] ++;
        x /= 10;
    }
    int g1 = 0, g2 = 0, t = 1;
    for(int i=0; i<10; i++)
        while(cnt[i]--)
        {
            g1 += i * t, g2 = g2 * 10 + i;
            t *= 10;
        }
    return g1 - g2;
}

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    while(k--) n = f(n);
    printf("%d\n", n);
    return 0;
}

D - Base n

Problem Statement

Given large integer $X$ (not fitting in long long) and $M$. Let $d$ be the maximum digit in $X$. Count how many $n > d$ satisfy that treating $X$ as a base-$n$ number yields a decimal value ≀ $M$.

$X$ has no leading zeros, 1 to 60 digits.
$1\le M\le 10^{18}$

Input Format

$X$
$M$

Output Format

Print the count.

Sample

Samples omitted. Visit AtCoder for details.

Analysis

Valid $n$ range: $d < n \le \text{max possible}$. Use binary search to find the maximum $n$ and subtract $d$.

Code

Key points:

  • Binary search boundaries
  • Overflow handling during value computation
  • Edge case when $X$ is single-digit

Without further ado, let’s dive into the code! ↓~~~~~~↓~~~~~~↓

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstdio>
using namespace std;

typedef unsigned long long ULL;

char x[65];
ULL m;

inline void setmax(int& x, int y)
{
    if(y > x) x = y;
}

inline bool check(const ULL& base)
{
    // Returns: (x -> base) <= m?
    ULL t = 0ULL;
    for(int i=0; x[i]; i++)
    {
        if(t > m / base)
            return false;
        t *= base;
        if((t += x[i] - '0') > m)
            return false;
    }
    return true;
}

int main()
{
    scanf("%s%llu", x, &m);
    int d = 0;
    for(int i=0; x[i]; i++)
        setmax(d, x[i] - '0');
    if(x[1] == '\0')
    {
        puts(d > m? "0": "1");
        return 0;
    }
    ULL l = d, r = m;
    while(l < r)
    {
        ULL mid = l + r + 1ULL >> 1ULL;
        if(check(mid)) l = mid;
        else r = mid - 1ULL;
    }
    printf("%llu\n", l - d);
    return 0;
}
Built with Hugo
Theme Stack designed by Jimmy