AtCoder Beginner Contest 190 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 - Very Very Primitive Game

Problem Statement

Takahashi and Aoki are playing a game. The rules are as follows:

  • Initially, Takahashi and Aoki have $A$ and $B$ candies, respectively.
  • They take turns eating one candy. The first player unable to eat loses. If $C=0$, Takahashi goes first; if $C=1$, Aoki goes first.

Output the winner’s name.

$0\le A,B\le 100$
$C \in \{0,1\}$

Input Format

$A~B~C$

Output Format

Print the winner.

Sample Cases

A B C Output
2 1 0 Takahashi
2 2 0 Aoki
2 2 1 Takahashi

Analysis

If Aoki starts first ($C=1$), he wins when $B > A$. For Takahashi starting first ($C=0$), increment $B$ by 1 to simulate Aoki’s first move scenario, then check if $B > A$.

Code

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

int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if(c == 0) b ++;
    puts(b > a? "Aoki": "Takahashi");
    return 0;
}

B - Magic 3

Problem Statement

A magician battles a monster. He can use $N$ spells. The $i$-th spell has cooldown $X_i$ seconds and deals $Y_i$ damage. The monster is immune to spells with cooldown $\geq S$ or damage $\leq D$. Can the magician damage the monster?

$1\le N\le 100$
$1\le X_i, Y_i\le 10^9$
$1\le S, D\le 10^9$

Input Format

$N~S~D$
$X_1~Y_1$
$X_2~Y_2$
$\vdots$
$X_N~Y_N$

Output Format

Print Yes if possible, otherwise No.

Sample Cases

Sample Input 1

1
2
3
4
5
4 9 9
5 5
15 5
5 15
15 15

Sample Output 1

1
Yes

With $S=D=9$:

Spell No. Cooldown Damage Can Damage Monster?
1 5 sec βœ“ 5 βœ— βœ—
2 15 sec βœ— 5 βœ— βœ—
3 5 sec βœ“ 15 βœ“ βœ“
4 15 sec βœ— 15 βœ“ βœ—

Sample Input 2

1
2
3
4
3 691 273
691 997
593 273
691 273

Sample Output 2

1
No

Sample Input 3

1
2
3
4
5
6
7
8
7 100 100
10 11
12 67
192 79
154 197
142 158
20 25
17 108

Sample Output 3

1
Yes

Only the 7th spell works.

Analysis

For each spell, check if $X_i < S$ and $Y_i > D$ hold. Output Yes if any spell satisfies both conditions.

Code

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

int main()
{
    int n, s, d;
    scanf("%d%d%d", &n, &s, &d);
    while(n--)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        if(x < s && y > d)
        {
            puts("Yes");
            return 0;
        }
    }
    puts("No");
    return 0;
}

C - Bowls and Dishes

Problem Statement

There are $N$ dishes numbered $1,2,\dots,N$ and $M$ conditions. Condition $i$ is satisfied if both dishes $A_i$ and $B_i$ contain at least one ball. $K$ people each place a ball in dish $C_i$ or $D_i$. Find the maximum number of satisfied conditions.

$2\le N\le 100$
$1\le M\le 100$
$1\le A_i < B_i\le N$
$1\le K\le 16$
$1\le C_i < D_i\le N$

Input Format

$N~M$
$A_1~B_1$
$\vdots$
$A_M~B_M$
$K$
$C_1~D_1$
$\vdots$
$C_K~D_K$

Output Format

Print the maximum count.

Sample Cases

Sample Input 1

1
2
3
4
5
6
7
8
9
4 4
1 2
1 3
2 4
3 4
3
1 2
1 3
2 3

Sample Output 1

1
2

Choosing dishes 1, 3, 2 satisfies conditions 1 and 2.

Sample Input 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
4 4
1 2
1 3
2 4
3 4
4
3 4
1 2
2 4
2 4

Sample Output 2

1
4

Choosing dishes 3, 1, 2, 4 satisfies all conditions.

Sample Input 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
6 12
2 3
4 6
1 2
4 5
2 6
1 5
4 5
1 3
1 2
2 6
2 3
2 5
5
3 5
1 4
2 6
4 6
5 6

Sample Output 3

1
9

Analysis

Enumerate all $2^K$ choices using bitmasking. For each combination, track which dishes have balls and count satisfied conditions. Time complexity is $\mathcal O(M2^K)$.

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
#include <cstdio>
#include <cstring>
using namespace std;

int a[105], b[105], c[20], d[20];

int main()
{
    int n, m, k;
    scanf("%d%d", &n, &m);
    for(int i=0; i<m; i++)
    {
        scanf("%d%d", a + i, b + i);
        a[i] --, b[i] --;
    }
    scanf("%d", &k);
    for(int i=0; i<k; i++)
    {
        scanf("%d%d", c + i, d + i);
        c[i] --, d[i] --;
    }
    int limit = 1 << k, ans = 0;
    for(int st=0; st<limit; st++)
    {
        bool hasdish[105] = {false};
        for(int i=0; i<k; i++)
            if(st & (1 << i))
                hasdish[c[i]] = true;
            else hasdish[d[i]] = true;
        int cnt = 0;
        for(int i=0; i<m; i++)
            cnt += hasdish[a[i]] && hasdish[b[i]];
        if(cnt > ans) ans = cnt;
    }
    printf("%d\n", ans);
    return 0;
}

D - Staircase Sequences

Problem Statement

Count the number of arithmetic sequences with sum $N$ and common difference 1 (allowing negative terms).

$1\le N\le 10^{12}$

Input Format

$N$

Output Format

Print the count.

Sample Cases

N Output
12 4
1 2
63761198400 1920

Analysis

$$\frac {(a+b)(b-a+1)} 2 = N$$$$(a+b)(b-a+1) = 2N$$


We count pairs of factors of $2N$ with different parity (one even, one odd). Multiply the result by 2 to account for negative sequences. Time complexity is $\mathcal O(\sqrt N)$.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <cstdio>
using namespace std;

typedef long long LL;

int main()
{
    LL n;
    scanf("%lld", &n);
    n <<= 1LL;
    int cnt = 0;
    for(LL i=1; i*i<=n; i++)
        if(n % i == 0 && i % 2 != n / i % 2)
            cnt ++;
    printf("%d\n", cnt << 1);
    return 0;
}
Built with Hugo
Theme Stack designed by Jimmy