AtCoder题解 —— AtCoder Beginner Contest 182 —— C - To 3

本文针对AtCoder初学者竞赛182-C题提供了解决方案。通过对输入数字N的每位进行分析,判断是否能通过删除部分位数使其成为3的倍数,并给出最少删除位数。采用模3的方法优化计算过程。
部署运行你感兴趣的模型镜像

题目相关

题目链接

AtCoder Beginner Contest 182 C 题,https://atcoder.jp/contests/abc182/tasks/abc182_c

Problem Statement

Given is a positive integer N, where none of the digits is 0.
Let k be the number of digits in N. We want to make a multiple of 3 by erasing at least 0 and at most k−1 digits from N and concatenating the remaining digits without changing the order.
Determine whether it is possible to make a multiple of 3 in this way. If it is possible, find the minimum number of digits that must be erased to make such a number.

Input

Input is given from Standard Input in the following format:

N

Output

If it is impossible to make a multiple of 3, print -1; otherwise, print the minimum number of digits that must be erased to make such a number.

Samples1

Sample Input 1

35

Sample Output 1

1

Explaination

By erasing the 5, we get the number 3, which is a multiple of 3. Here we erased the minimum possible number of digits - 1.

Samples2

Sample Input 2

369

Sample Output 2

0

Explaination

Note that we can choose to erase no digit.

Samples3

Sample Input 3

6227384

Sample Output 3

1

Explaination

For example, by erasing the 8, we get the number 622734, which is a multiple of 3.

Samples4

Sample Input 4

11

Sample Output 4

-1

Explaination

Note that we must erase at least 0 and at most k−1 digits, where k is the number of digits in N, so we cannot erase all the digits.
In this case, it is impossible to make a multiple of 3 in the way described in the problem statement, so we should print -1.

Constraints

  • 1≤N<10^18
  • None of the digits in N is 0.

题解报告

题目翻译

给定一个正整数 N,数据中没有数字 0。问如果 N 不能被 3 整除,我们可以从 0 到 k-1 位中,任意删除,找出最小的删除位数。

样例数据分析

这个题目含义比较明确,我们不需要分析样例数据。

题目分析

我们知道,判断一个数字能否被 3 整除,我们只需要将该数字的所有构成数字求和,如何这个和可以被 3 整除,那么这个数肯定可以被 3 整除。N 的最大值是 1e18,因此 long long 是可以表示的。本题可以使用 getchar() 逐一读入每个位。

由于是判断能否被 3 整除,因此 0、3、6、9 是等效的;1、4、7 是等效的;2、5、8 是等效的。也就是每位数字 p 和 p%3 是等效的。这样我们可以设计出如下的算法:

1、逐位读入数据。逐一要忽略回车,也就是 '\n' 字符。将读取的数字记为 p;

2、将所有的 p 求和,记为 sum;

3、记录每位 p%3 的个数;

4、记录数据的位数,记为 tot;

5、分类讨论结果。

  • sum 可以被 3 整数,说明数据 n 可以被 3 整数;
  • sum%3 得到 1。说明 n 多了一个 1 相关位(对应的真实数据可以是 1、4、7)。我们只需要删除的方案有两种:1、当数据位数超过 1 的时候,删除一个 1;2、当数据位数超过 2 的时候,删除两个 2;3、上面两个可能都没有,就无法达成预期。
  • sum%3 得到 2。说明 n 多了一个 2 相关位(对应的真实数据可以是 2、5、8)。我们只需要删除的方案有两种:1、当数据位数超过 1 的时候,删除一个 2;2、当数据位数超过 2 的时候,删除两个 1;3、上面两个可能都没有,就无法达成预期。

AC 参考代码

//https://atcoder.jp/contests/abc182/tasks/abc182_c
//C - To 3
#include <bits/stdc++.h>

using namespace std;

int main() {
    long long n;
    cin>>n;

    //特判
    if (0==n%3) {
        cout<<"0\n";
        return 0;
    } else if (n<=2) {
        cout<<"-1\n";
        return 0;
    }

    //转换为数字
    int cnt[3]={};//每个数字个数
    int sum=0;//每个位数总和
    int tot=0;//位数
    while (n) {
        int p=n%10;//取个位
        n/=10;
        sum+=p;
        tot++;
        cnt[p%3]++;
    }

    if (1==sum%3) {
        if (tot>1 && cnt[1]>=1) {
            cout<<"1\n";
        } else if (tot>2 && cnt[2]>=2) {
            cout<<"2\n";
        } else {
            cout<<"-1\n";
        }
    } else if (2==sum%3) {
        if (tot>1 && cnt[2]>=1) {
            cout<<"1\n";
        } else if (tot>2 && cnt[1]>=2) {
            cout<<"2\n";
        } else {
            cout<<"-1\n";
        }
    }

    return 0;
}

时间复杂度

O(N)。

您可能感兴趣的与本文相关的镜像

Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

<think>好的,用户现在想找AtCoderBeginner Contest408的题解或解思路。我需要先确认这个比赛是否已经结束,以及是否有公开的题解资源。由于AtCoder通常会在比赛后一段时间内发布官方题解,但有时候社区成员或博客作者也会分享他们的解方法。首先,我应该检查用户提供的引用内容,但看起来用户之前的引用是关于其他比赛的,比如ABC093ABC405,可能与当前问无关。不过,用户可能需要类似的解思路结构,比如分讲解、代码示例等。接下来,我需要考虑用户可能的真实需求。他们可能参加了比赛但遇到困难,或者想通过题解来学习算法和编程技巧。因此,回答需要结构清晰,分目讲解思路,并给出关键点的解决方法,可能的话提供代码示例。 由于我无法直接访问外部网站,无法提供最新的ABC408的具体题解,但可以给出一般性的解步骤和常见问的解决方法。例如,对于ABC目,通常前几涉及基础算法、模拟、数学问等,后面目可能涉及动态规划、图论等更复杂的算法。需要注意的是,用户可能希望得到具体的解思路,比如每道的关键点、算法选择以及代码实现。因此,我应该分目进行说明,可能按照A到F的顺序,逐一简要讲解。另外,用户可能需要参考之前的类似题解的格式,比如分说明、代码示例等。同时,根据用户之前的引用中提到的代码结构,可能需要使用Python或C++等语言的代码示例。最后,生成的相关问应该围绕如何准备AtCoder比赛、常见型解析、算法学习资源等,帮助用户进一步深入学习。</think>### AtCoder Beginner Contest 408 题解思路概述 根据AtCoder比赛惯例,ABC 408包含6道目(A-F),难度递增。以下为通用解框架: #### A(基础语法) 通常考察输入输出和简单计算。例如: ```python a, b = map(int, input().split()) print(a + b % 24) ``` **关键点**:注意时间循环处理[^1] #### B(条件判断) 可能涉及字符串处理或坐标判断。例如检测网格中的特定模式: ```python grid = [input() for _ in range(9)] count = 0 for i in range(9): for j in range(9): if grid[i][j] == &#39;#&#39; and check_pattern(i,j): count +=1 print(count) ``` #### C(贪心/数学) 常需数学建模,如求最大最小值的排列组合: $$ \max\left(\sum_{i=1}^n a_i \cdot b_{\sigma(i)}\right) $$ 可通过排序后对应相乘解决[^2] #### D(图论/动态规划) 典型解法示例: ```python from collections import deque def bfs(start): q = deque([start]) dist = [-1]*(n+1) dist[start] = 0 while q: u = q.popleft() for v in graph[u]: if dist[v] == -1: dist[v] = dist[u]+1 q.append(v) return max(dist) ``` #### E-F(高级算法) 可能涉及: 1. 线段树区间查询 2. 网络流建模 3. 组合数学优化
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值