Small Multiple
生活随笔
收集整理的這篇文章主要介紹了
Small Multiple
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
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.
Constraints
2≤K≤105
K is an integer.
輸入
Input is given from Standard Input in the following format:
K
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.
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=3e5+;
const int p=1e9+;
int k,cnt;
int dis[N],last[N];
bool vis[N];
struct orz{
int v,nex,s;}e[N*];
queue<int>q;
void add(int x,int y,int z)
{
cnt++;
e[cnt].v=y;
e[cnt].nex=last[x];
last[x]=cnt;
e[cnt].s=z;
}
void spfa()
{
for (int i=;i<k;i++) dis[i]=inf;
dis[]=; vis[]=;
q.push();
while (!q.empty())
{
int now=q.front(); q.pop();
for (int i=last[now];i;i=e[i].nex)
{
if (dis[e[i].v]>dis[now]+e[i].s)
{
dis[e[i].v]=dis[now]+e[i].s;
if (!vis[e[i].v])
{
vis[e[i].v]=;
q.push(e[i].v);
}
}
}
vis[now]=;
}
}
int main()
{
scanf("%d",&k);
for (int i=;i<k;i++)
{
add(i,(i+)%k,);
add(i,i*%k,);
} spfa(); printf("%d\n",dis[]+);
return ;
}
總結
以上是生活随笔為你收集整理的Small Multiple的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL-14-备份恢复
- 下一篇: Linux - fuser 命令