51nod 1109 01组成的n的倍数

本文介绍了一种使用广度优先搜索(BFS)算法解决特定数学问题的方法:给定一个自然数N,寻找最小的正整数M,使得M既是N的倍数,又仅由0和1组成。


原题链接

51nod 1109
题目类型: 4 4 4级题 ♦ ♦ ♦ ♦ {\color{green}{♦♦}}{\color{lightgreen}{♦♦}}{\color{yellow}{}}{\color{orange}{}}{\color{red}{}}
AC记录:Accepted

题目大意

给定一个自然数 N N N,找出一个 M M M,使得 M > 0 M >0 M>0 M M M N N N的倍数,并且 M M M 10 10 10进制表示只包含 0 0 0 1 1 1。求最小的 M M M

输入格式

输入 1 1 1个数 N N N

输出格式

输出符合条件的最小的M。
S a m p l e \mathbf{Sample} Sample I n p u t \mathbf{Input} Input

4

S a m p l e \mathbf{Sample} Sample O u t p u t \mathbf{Output} Output

100

H i n t & E x p l a i n \mathbf{Hint\&Explain} Hint&Explain

数据范围

对于 100 % 100\% 100%的数据, 1 ≤ N ≤ 1 0 6 1\le N\le 10^6 1N106

解题思路

此题可以用 b f s bfs bfs来解决。
我们知道,这题求的是最短的长度,所以我们可以用广搜,搜索每一个状态。
最初,状态从 1 1 1开始,然后每一次扩展,都是在这个数的后面加上一个 0 0 0或者 1 1 1
看起来很简单,就像一道简简单单的板子题一样。
可是,这里的 n n n的范围可以达到 1 0 6 10^6 106,所以我们需要一些优化。
既然题目要求的是 m m m n n n的倍数,换一种说法就是 m m m能被 n n n整除, m = 0 (   m o d     n ) m=0(\bmod\ n) m=0(mod n)。所以我们只需要记录每一个状态   m o d     n \bmod\ n mod n的值,以及判重,我们就可以解出此题。
下面说一下状态转移的要点。
在状态转移时,是在后面加上一个 0 0 0 1 1 1,所以设当前状态为 n o w now now,则接下来的状态为 10 × n o w 10\times now 10×now 10 × n o w + 1 10\times now+1 10×now+1。再根据同余原理,就可以把 × 10 \times 10 ×10 + 1 +1 +1拎出来,进而去重,最后得到答案。

注意事项:

1.在去重的时候,判断的是   m o d     n \bmod\ n mod n意义下的余数,不是这个数本身!
2.在状态转移时,记得要把存储原数字的一个vector也一起转移!

最后输出的时候,输出 f 0 f_0 f0中保存数字的vector就行。


拓展练习:
P2841 A*B Problem
这一题是在本题的基础上加强了许多,但是据洛谷上某位神犇说,此题可以用__int128_t水过,所以下面放的是P2841的代码,不是51nod 1109的代码


最后,祝大家早日
AC

上代码

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef __int128_t int128;

struct obj{
    obj(int128 a=0,int b=0):times(a),left(b){}
    vector<int> bits;
    ull times;
    int left;
};

queue<obj>     q;
bool           a[1000010];
int            n;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    if(n==1)
    {
        cout<<"1 1"<<endl;
        return 0;
    }
    obj st=obj(0,1);
    st.bits.push_back(1);
    q.push(st);
    while(q.size())
    {
        obj now=q.front();
        q.pop();
        int times=now.times,left=now.left;
        if(left==0)
        {
            // cout<<times<<" ";
            for(int i=0; i<now.bits.size(); i++)
                cout<<now.bits[i];
            cout<<endl;
            return 0;
        }
        for(int i=0; i<=1; i++)
        {
            int next_state=left*10+i;
            if(a[next_state%n]==true)
                continue;
            obj temp=obj(times*10+next_state/n,next_state%n);
            temp.bits=now.bits;
            temp.bits.push_back(i);
            a[next_state%n]=true;
            q.push(temp);
        }
    }
    return 0;
}

完美切题 ∼ \sim

题目 51nod 3478 涉及一个矩阵问题,要求通过最少的操作次数,使得矩阵中至少有 `RowCount` 行和 `ColumnCount` 列是回文的。解决这个问题的关键在于如何高效地枚举所有可能的行和列组合,并计算每种组合所需的操作次数。 ### 解法思路 1. **预处理每一行和每一列变为回文所需的最少操作次数**: - 对于每一行,计算将其变为回文所需的最少操作次数。这可以通过比较每对对称位置的值是否相同来完成。 - 对于每一列,计算将其变为回文所需的最少操作次数,方法同上。 2. **枚举所有可能的行和列组合**: - 由于 `N` 和 `M` 的最大值为 8,因此可以枚举所有可能的行组合和列组合。 - 对于每一种组合,计算其所需的最少操作次数,并取最小值。 3. **计算操作次数**: - 对于每一种组合,需要计算哪些行和列需要修改,并且注意行和列的交叉点可能会重复计算,因此需要去重。 ### 代码实现 以下是一个可能的实现方式,使用了枚举和位运算来处理组合问题: ```python def min_operations_to_palindrome(matrix, row_count, col_count): import itertools N = len(matrix) M = len(matrix[0]) # Precompute the cost to make each row a palindrome row_cost = [] for i in range(N): cost = 0 for j in range(M // 2): if matrix[i][j] != matrix[i][M - 1 - j]: cost += 1 row_cost.append(cost) # Precompute the cost to make each column a palindrome col_cost = [] for j in range(M): cost = 0 for i in range(N // 2): if matrix[i][j] != matrix[N - 1 - i][j]: cost += 1 col_cost.append(cost) min_total_cost = float('inf') # Enumerate all combinations of rows and columns rows = list(range(N)) cols = list(range(M)) from itertools import combinations for row_comb in combinations(rows, row_count): for col_comb in combinations(cols, col_count): # Calculate the cost for this combination cost = 0 # Add row costs for r in row_comb: cost += row_cost[r] # Add column costs for c in col_comb: cost += col_cost[c] # Subtract the overlapping cells for r in row_comb: for c in col_comb: # Check if this cell is part of the palindrome calculation if r < N // 2 and c < M // 2: if matrix[r][c] != matrix[r][M - 1 - c] and matrix[N - 1 - r][c] != matrix[N - 1 - r][M - 1 - c]: cost -= 1 min_total_cost = min(min_total_cost, cost) return min_total_cost # Example usage matrix = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ] row_count = 2 col_count = 2 result = min_operations_to_palindrome(matrix, row_count, col_count) print(result) ``` ### 代码说明 - **预处理成本**:首先计算每一行和每一列变为回文所需的最少操作次数。 - **枚举组合**:使用 `itertools.combinations` 枚举所有可能的行和列组合。 - **计算成本**:对于每一种组合,计算其成本,并考虑行和列交叉点的重复计算问题。 ### 复杂度分析 - **时间复杂度**:由于 `N` 和 `M` 的最大值为 8,因此枚举所有组合的时间复杂度为 $ O(N^{RowCount} \times M^{ColCount}) $,这在实际中是可接受的。 - **空间复杂度**:主要是存储预处理的成本,空间复杂度为 $ O(N + M) $。 ### 相关问题 1. 如何优化矩阵中行和列的枚举组合以减少计算时间? 2. 在计算行和列的交叉点时,如何更高效地处理重复计算的问题? 3. 如果矩阵的大小增加到更大的范围,如何调整算法以保持效率? 4. 如何处理矩阵中行和列的回文条件不同时的情况? 5. 如何扩展算法以支持更多的操作类型,例如翻转某个区域的值?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值