本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/15839565.html

题目链接

题目

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

给定一个整数K.求一个K的整数倍SUM,使得SUM的数位累加和最小

思路

考虑翻倍。

如果一个数翻10倍,那么这个数位之和不变。

如果它翻 (10+k)(10+k)(k9)(k\leqslant 9),数位之和加上 kk

根据这个原理,对于 [0,k1][0, k-1] 的点,我们可以连出10条边。

然后我们把 [1,9][1,9] 号点点权为本身,然后跑一遍最短路,最后答案就是 ans0ans_0

总结

这是一道很好的建图题。

这道题的核心思路是翻倍,如果翻两倍,数位之和会难以估计。

而在十进制下,通过翻10倍能巧妙解决这个问题。

基于这个原理,建图后跑最短路即可求出答案。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Problem: D - Small Multiple
// Contest: AtCoder - AtCoder Regular Contest 084
// URL: https://atcoder.jp/contests/arc084/tasks/arc084_b
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
#define N 100010
struct Node
{
int x, y;
bool operator <(const Node &A) const
{
return x>A.x;
}
}t;
struct node
{
int x, y, z, n;
}d[N*10];
int n, m, i, j, k;
int u, v, g;
int ans[N], h[N], b[N];
priority_queue<Node>q;

int sum(int x)
{
int ans=0;
while(x) ans+=x%10, x/=10;
return ans;
}

void cun(int x, int y, int z)
{
// printf("cun(%lld, %lld)=%lld\n", x, y, z);
d[++k].x=x; d[k].y=y; d[k].z=z;
d[k].n=h[x]; h[x]=k;
}

signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
memset(h, -1, sizeof(h));
n=read();
for(i=0; i<n; ++i)
{
for(j=0; j<=9; ++j)
cun(i, (i*10+j)%n, j);
}
q.push(Node{0, ans[0]=sum(n)}); b[0]=1;
for(i=1; i<n; ++i) q.push(Node{i, ans[i]=sum(i)}), b[i]=1;
while(!q.empty())
{
t=q.top(); q.pop();
b[u=t.x]=0;
for(g=h[u]; g!=-1; g=d[g].n)
{
v=d[g].y;
if(ans[u]+d[g].z<ans[v])
{

ans[v]=ans[u]+d[g].z;
// printf("%lld->%lld\n", u, v, ans[v]);
if(!b[v])
{
b[v]=1;
q.push(Node{v, ans[v]});
}
}
}
}
// for(i=0; i<n; ++i) printf("%lld ", ans[i]);
// printf("\n");
printf("%lld", ans[0]);
return 0;
}