Yet Another Multiple Problem(BFS)

本文探讨了一个关于整数倍数的挑战性问题,即找出一个正整数的最小倍数,该倍数的十进制表示不包含指定的一组数字。通过使用广度优先搜索(BFS)算法,实现高效求解。

Yet Another Multiple Problem

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3041    Accepted Submission(s): 735


Problem Description
There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”.
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
 

 

Input
There are several test cases.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 10 4). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
 

 

Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or “-1” (without quotation marks) in case there does not exist such a multiple.
 

 

Sample Input
2345 3
7 8 9
100
1 0
 

 

Sample Output
Case 1: 2345
Case 2: -1
 

 

Source

 

 

        题意:

        给出 N(1 ~ 10000) 和 M,后给出 M 个数字,输出 N 的最小倍数,这个数不包含给出的 M 个数字。

 

       思路:

       BFS。枚举 N 的倍数的话无疑会 T,所以从 0 ~ 9 构造数,判断这个数能否出现且维护这个数 % N 的余数,以第一个出现的余数为基准,因为以后出现相同余数的一定会比之前的大,所以第一次出现的余数就放进队列中,知道找到 % N == 0 的为止。下次更新新的数的时候,mod = (mod X 10 + i )% n。注意插入第一个数的时候也要判断 % N 后是不是等于 0。

 

        AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <iostream>

using namespace std;

typedef struct {
        int mod;
        string num;
} node;

bool vis[15], p[10005];
int num;

bool bfs () {
        queue<node> q;

        for (int i = 1; i <= 9; ++i) {
                if (!vis[i]) {
                        node a;
                        a.mod = i % num;
                        a.num = i + '0';
                        if (!a.mod) {
                                cout << a.num << endl;
                                return true;
                        }
                        q.push(a);
                }
        }

        while (!q.empty()) {
                node now = q.front(); q.pop();

                for (int i = 0; i <= 9; ++i) {
                        node New;

                        if (!vis[i]) {
                                string a;
                                a = i + '0';

                                New.num = now.num + a;
                                New.mod = (now.mod * 10 + i) % num;
                                if (!New.mod) {
                                        cout << New.num << endl;
                                        return true;
                                } else if (!p[New.mod]) {
                                        q.push(New);
                                        p[New.mod] = 1;
                                }
                        }
                }
        }

        return false;
}

int main() {
        int m, t = 0;

        while (~scanf("%d%d", &num, &m)) {
                memset(vis, 0, sizeof(vis));
                memset(p, 0, sizeof(p));

                while (m--) {
                        int ans;
                        scanf("%d", &ans);
                        vis[ans] = 1;
                }

                printf("Case %d: ", ++t);
                if(!bfs()) printf("-1\n");
        }

        return 0;
}

 

 

 

Problem Statement There is a simple undirected graph with N vertices and M edges. The graph consists of vertex 1, vertex 2,…, vertex N, and the i-th edge (1≤i≤M) connects vertices u i ​ and v i ​ . You will perform the following operation zero or more times: Choose one edge that has not been deleted yet, and delete it. Your goal is to make the graph bipartite. Find the minimum number of operations needed to make the graph after the operations bipartite. What it means for a graph to be simple?    A graph is simple if and only if it has no self-loops (edges where u i ​ =v i ​ ) or multi-edges (pairs of edges where u i ​ =u j ​ and v i ​ =v j ​ ). DeepL 翻译    图形简单意味着什么? 当且仅当一个图没有自循环( u i ​ =v i ​ 的边)或多对边( u i ​ =u j ​ 和 v i ​ =v j ​ 的边对)时,这个图才是简单的。 What is a bipartite graph?    A bipartite graph is a graph where it is possible to color each vertex black or white satisfying the following condition: For every edge, the two vertices connected by that edge have different colors. DeepL 翻译    什么是二方图? 二方图是一种可以将每个顶点涂成黑色或白色的图,并满足以下条件: 对于每一条边,由该边连接的两个顶点具有不同的颜色。 DeepL 翻译    问题陈述 有一个简单的无向图,该图有 N 个顶点和 M 条边。该图由顶点 1, 顶点 2,…, 顶点 N 和连接顶点 u i ​ 和 v i ​ 的 i -th 边 (1≤i≤M) 组成。 您将执行以下操作零次或多次: 选择一条尚未删除的边,并删除它。 您的目标是使图形成为两部分。找出使操作后的图形成为两部分所需的最少操作次数。 图形简单意味着什么? 只有当且仅当一个图没有自循环( u i ​ =v i ​ 的边)或多对边( u i ​ =u j ​ 和 v i ​ =v j ​ 的边对)时,这个图才是简单的。 什么是二方图? 二方图是指每个顶点都可以涂成黑色或白色,并满足以下条件的图: 对于每一条边,由该边连接的两个顶点具有不同的颜色。    Constraints 2≤N≤10 1≤M≤ 2 N(N−1) ​ 1≤u i ​ <v i ​ ≤N (1≤i≤M) The given graph is simple. All input values are integers. DeepL 翻译    限制因素 2≤N≤10 1≤M≤ 2 N(N−1) ​ 1≤u i ​ <v i ​ ≤N (1≤i≤M) 给定的图形很简单。 所有输入值均为整数。    Input The input is given from Standard Input in the following format: N M u 1 ​ v 1 ​ u 2 ​ v 2 ​ ⋮ u M ​ v M ​ DeepL 翻译    输入 输入内容由标准输入法提供,格式如下 N M u 1 ​ v 1 ​ u 2 ​ v 2 ​ ⋮ u M ​ v M ​    Output Print the number of operations that need to be performed to make the graph bipartite. DeepL 翻译    输出 打印将图形变为两部分所需的操作数。    Sample Input 1 Copy 5 8 1 2 1 3 1 4 2 3 2 5 3 4 3 5 4 5 DeepL 翻译    输入样本 1 5 8 1 2 1 3 1 4 2 3 2 5 3 4 3 5 4 5    Sample Output 1 Copy 2 You can make the graph bipartite by deleting two edges: for example, the edge connecting vertices 1 and 3, and the edge connecting vertices 3 and 5. It is impossible to make the graph bipartite by performing one or less operations, so print 2. DeepL 翻译    样本输出 1 2 您可以通过删除两条边来使图形变成两部分:例如,删除连接顶点 1 和 3 的边,以及删除连接顶点 3 和 5 的边。 只进行一次或更少的操作是不可能使图形变成两部分的,因此打印 2。    Sample Input 2 Copy 2 1 1 2 DeepL 翻译    输入样本 2 2 1 1 2    Sample Output 2 Copy 0 The graph is bipartite from the beginning. Thus, the number of operations that need to be performed is 0. DeepL 翻译    输出示例 2 0 该图从一开始就是二叉图。因此,需要执行的操作数为 0 。    Sample Input 3 Copy 10 20 5 9 1 4 3 8 1 6 4 10 5 7 5 6 3 7 3 6 5 10 1 3 3 4 6 7 1 2 4 7 1 5 1 9 9 10 4 5 8 9 DeepL 翻译    输入样本 3 10 20 5 9 1 4 3 8 1 6 4 10 5 7 5 6 3 7 3 6 5 10 1 3 3 4 6 7 1 2 4 7 1 5 1 9 9 10 4 5 8 9    Sample Output 3 Copy 5 DeepL 翻译    输出示例 3 5
10-12
冰淇淋机(perimeter)【问题描述】 Farmer John 要开始他的冰激凌生意了!他制造了一台可以生产冰激凌球的机器,然而不幸的是形状不太规则,所以他现在希望优化一下这台机器,使其产出的冰激凌球的形状更加合理。 机器生产出的冰激凌的形状可以用一个 N×N(1≤N≤1000)的矩形图案表示,例如:##.... ....#. .#..#. .##### ...### ....## 每个'.'字符表示空的区域,每个'#'字符表示一块 1×1 的正方形格子大小的冰激凌。不幸的是,机器当前工作得并不是很正常,可能会生产出多个互不相连的冰激凌球(上图中有两个)。一个冰激凌球是连通的,如果其中每个冰激凌的正方形格子都可以从这个冰激凌球中其他所有的冰激凌格子出发重复地前往东、南、西、北四个方向上相邻的冰激凌格子所到达。 Farmer John 想要求出他的面积最大的冰激凌球的面积和周长。冰激凌球的面积就是这个冰激凌球中'#'的数量。如果有多个冰激凌球并列面积最大,他想要知道其中周长最小的冰激凌球的周长。在上图中,小的冰激凌球的面积为 2,周长为 6,大的冰激凌球的面积为 13,周长为 22。 注意一个冰激凌球可能在中间有“洞”(由冰激凌包围着的空的区域)。如果这样,洞的边界同样计入冰激凌球的周长。冰激凌球也可能出现在被其他冰激凌球包围的区域内,在这种情况下它们计为不同的冰激凌球。例如,以下这种情况包括一个面积为 1 的冰激凌球,被包围在一个面积为 16 的冰激凌球内:##### #...# #.#.# #...# ##### 同时求得冰激凌球的面积和周长十分重要,因为 Farmer John 最终想要最小化周长与面积的比值,他称这是他的冰激凌的“冰周率”。当这个比率较小的时候,冰激凌化得比较慢,因为此时冰激凌单位质量的表面积较小。【输入格式】 从文件 perimeter.in 中读入数据。 输入的第一行包含 N,以下 N 行描述了机器的生产结果。其中至少出现一个'#'字符。【输出格式】 输出到文件 perimeter.out 中。 输出一行,包含两个空格分隔的整数,第一个数为最大的冰激凌球的面积,第二个数为它的周长。如果多个冰激凌球并列面积最大,输出其中周长最小的那一个的信息。 输入样例 1: 6 ##.... ....#. .#..#. .##### ...### ....## 输出样例 1:13 22 输入样例 2: 选手目录 perimeter/perimeter2.in 和 perimeter/perimeter2.ans
10-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值