Time limit : 2sec
Memory limit : 256MB
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
题意:给定数字k,求问:在十进制下,各个位数字的和最小的k*i是多少(i是正整数,k<=100000)
题解:题解太神了,先膜
从数字0开始,对于每一个数字x,向x+1连一条权值为1的单向边,向x*10连一条权值为0的边,如果x+1或者x *10大于k的话,那就对k取模。
神奇的结论,答案就是1+点1到点0的最短路。
贴题解以后慢慢看:https://img.atcoder.jp/arc084/editorial.pdf (page8)
#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
int k,ne,h[100004];
struct edge{
int to,nt,w;
}e[500004];
queue<int>q;
int dis[100004];
void add(int u,int v,int w){
e[++ne].to = v;e[ne].nt = h[u];
e[ne].w=w;
h[u]=ne;
}
int LiangJiaJun(){
scanf("%d",&k);
for(int i=0;i<k;i++){
add(i,(i+1)%k,1);
add(i,(i*10)%k,0);
}
q.push(1);
memset(dis,127/3,sizeof(dis));
dis[1]=0;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=h[x];i;i=e[i].nt){
if( dis[e[i].to]>dis[x]+e[i].w){
dis[e[i].to]=dis[x]+e[i].w;
q.push(e[i].to);
}
}
}
printf("%d\n",dis[0]+1);
return 0;
}