【AT3621 [ARC084B] Small Multiple】题解

题目链接

题目

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)\)\((k\leqslant 9)\),数位之和加上 \(k\)

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

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

总结

这是一道很好的建图题。

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

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

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

Code

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

posted @ 2022-01-24 15:23  zhangtingxi  阅读(97)  评论(0编辑  收藏  举报