问题
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.
Sample Input 3
79992
Sample Output 3
36
双向队列
deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,deque在接口上和vector非常相似,下面列出deque的常用成员函数:
解题
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
using namespace std;
typedef long long LL;
#define ms(a,b) memset(a,b,sizeof(a))
# define eps 1e-8
bool to(int &x, int y) {
if (x > y) {
x = y;
return true;
}
return false;
}
int main()
{
int K;
cin>>K;
deque<int> q;//双向队列
q.push_back(1);
vector<int> dp(K,1e9);//表示有K个值为1e9的数
dp[1] = 1;
while (!q.empty()) {
int u = q.front(); q.pop_front();
//int d = dp[u];
if (to(dp[u * 10 % K], dp[u])) {
q.push_front(u * 10 % K);
}
if (to(dp[(u + 1) % K], dp[u] + 1)) {
q.push_back((u + 1) % K);
}
}
cout << dp[0] << endl;
return 0;
}