【题解】[ABC077D] Small Multiple
题目大意
嗯对,这道题是01BFS
不会的去研读我的【学习笔记】01BFS - ExAll
好的,我觉mo得ren你学会了!!!
我们假设有\(X = 1\)
那么它可以有以下几项需要
- \(\times 10\) 花费为0
- $ + 1$花费为1
问题:会爆long long
我们发现我们欲求 K 的倍数,那么我们在每次 ×10 是 %K 就行了,因为这样余数不变,不影响结果。
这样就变成01BFS啦ヾ(≧▽≦*)o
挂个代码
#include <bits/stdc++.h>
using namespace std;
int n;
void bfs()
{
deque <pair <int, int>> q;
q.emplace_back(1, 1);
vector <int> v(n + 1, 0);
while (!q.empty())
{
auto [a, b] = q.front();
q.pop_front();
if (v[a]) continue;
v[a] = 1;
if (!a)
{
cout << b << endl;
return;
}
q.emplace_front((a * 10) % n, b);
q.emplace_back((a + 1) % n, b + 1);
}
}
int main()
{
cin >> n;
bfs();
return 0;
}

浙公网安备 33010602011771号