Small Multiple

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 is given from Standard Input in the following format:
K

 

输出

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

 

样例输入

6

 

样例输出

3

 

提示

12=6×2 yields the smallest sum.

 

输入一个数K,求K倍数,使得这个数各位的数的和最小,并输出最小是多少。

网上大多数代码实现都是搜索,奈何我搜索的基础较差,只能找到了一种偷懒的方式,尝试用抽屉原理。

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int vis[100050];
int main()
{
    int k;
    scanf("%d",&k);
    deque<P> q;
    q.push_back(make_pair(1,1));
    while(!q.empty())
    {
        P x=q.front();
        q.pop_front();
        if(vis[x.first])
        {
            continue;
        }
        vis[x.first]=1;
        if(x.first==0)//判断是不是倍数
        {
            printf("%d\n",x.second);
            break;
        }
        q.push_front(make_pair(x.first*10%k,x.second));//十倍
        q.push_back(make_pair((x.first+1)%k,x.second+1));//加一
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值