[AtCoder](3621)Small Multiple ---- 思维+BFS

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

  • 2≤K≤105
  • K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Sample Input 1

6

Sample Output 1

3

12=6×2 yields the smallest sum.

Sample Input 2

41

Sample Output 2

5

11111=41×271 yields the smallest sum.

题意:让你求K的某i倍,使得其各位数字的和最小。

做法:我们把答案设为X

X可以从1开始,每次选择做两种操作。

一:x*10,那么代价就是0,因为各位数字的和没有变

二:x+1,那么代价就是1,因为个位数字的和多了1

让后就是倍数问题,这时候我们可以转换成%K,比如K是6,12是6的倍数和12%6 = 0 是 等价的

所以问题就转换成了什么呢?利用BFS找到满足条件的X即可,同时要使用双端队列,让代价0的放在队首,代价1的放在队尾

感叹:别人的题解太巧了,世界上有很多比我介个蒟蒻优秀的人等着我去追赶_(:з」∠)_ 

AC代码:

#include<bits/stdc++.h>
#define rep(i,s,t) for(int i = (int)(s); i <= (int)(t); i++)
#define rev(i,t,s) for(int i = (int)(t); i >= (int)(s); i--)
#define pb push_back
#define sz(x) (int)(x).size()
using namespace std;
typedef pair<int,int> pii;
deque<pii> que;
const int maxn = 1e5+5;
bool vis[maxn];
int main()
{
    #ifdef LOCAL_FILE
    freopen("in.txt","r",stdin);
    #endif // LOCAL_FILE
    ios_base::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int k;
    cin>>k;
    que.push_front(make_pair(1,1));
    while(!que.empty())
    {
        pii now = que.front();
        que.pop_front();
        if(vis[now.first]) continue;
        vis[now.first] = true;
        if(now.first == 0)
        {
            cout<<now.second<<endl;
            break;
        }
        que.push_front(make_pair(now.first*10%k,now.second));
        que.push_back(make_pair((now.first+1)%k,now.second+1));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37624640/article/details/81449579