问题 C: Small Multiple
时间限制: 1 Sec 内存限制: 512 MB
提交: 311 解决: 19
[提交] [状态] [讨论版] [命题人:admin]
题目描述
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的倍数中,各位数字和最小的数并输出。
思路:
循环。dfs判断。
AC代码:
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
int k;
int vis[55][maxn];
int dfs(int x, int y){
if(x < 0) return 0;
if(x == 0 && y == 0)
return 1;
if(vis[x][y]) return 0;
vis[x][y] = 1;
for(int j = 0; j < 10; j++)
if(dfs(x - j, (y * 10 + j) % k))
return 1;
return 0;
}
int main()
{
scanf("%d", &k);
int n = k, s = 0;
while(n){
s += n % 10;
n /= 10;
}
int ans=s;
for(int i=1;i<s; i++) {
if(dfs(i, 0)) {
ans = i;
break;
}
}
printf("%d\n", ans);
return 0;
}