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;
}