Small Multiple(最短路)
6616: Small Multiple
時間限制: 1 Sec 記憶體限制: 512 MB
提交: 345 解決: 28
[提交] [狀態] [討論版] [命題人: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.
來源/分類
分析:這個題和圖論結合起來我也是很服氣,但是仔細想想也確實有道理。把數字和數字之間建邊,邊權對應數字之間的差值。
1、x與x+1建邊,邊權為1,因為各個位數之和相比與x加了1。x+1要%k。
2、x與x*10建邊,邊權為0,各個位數和不變。x*10%k。
3、跑最短路。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e6+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
struct node
{
int from, to, w;
} G[MAX];
int dist[MAX];
int head[MAX];
bool vis[MAX];
int cnt;
void add(int u, int v, int w)
{
G[cnt].to=v;
G[cnt].w=w;
G[cnt].from=head[u];
head[u]=cnt++;
}
int k;
void init()
{
int i,j;
memset(vis,0,sizeof(vis));
memset(head,-1,sizeof(head));
fill(dist,dist+MAX,100000);
scanf("%d",&k);
for(i=0;i<k;i++)
{
int a=i;
int b=(i+1)%k;
int c=i*10%k;
add(a,b,1);
add(a,c,0);
}
}
void spfa(int s)
{
memset(vis, false, sizeof(vis));
queue<int> q;
while(!q.empty())
q.pop();
vis[1]=true;
q.push(s);
dist[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u]; ~i; i=G[i].from)
{
int v=G[i].to;
if(dist[v]>dist[u]+G[i].w)
{
dist[v]=dist[u]+G[i].w;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
}
void output()
{
printf("%d\n",dist[0]+1);
}
int main()
{
init();
spfa(1);
output();
return 0;
}
相關文章
- 題解【[ABC077D] Small Multiple】(尚未完成)
- ABC077D / ARC084B Small Multiple 題解
- 最短路:求最長最短路
- 最短路 || 最長路 || 次短路
- C# split big file into small files as, and merge the small files into big oneC#
- JavaScript select multipleJavaScript
- Logstash Multiple Pipelines
- COMP2211A small shell interface
- However small the rewards may seem in comparison
- 2.3.6.2 Synchronization of Multiple ApplicationsAPP
- LLM multiple modal applicationsAPP
- kubernetes traefik multiple namespacesnamespace
- They just wanted to find small ways to throw people
- ORACLE _small_table_threshold與eventOracle
- jscalpel A small feature library that makes it easier to manipulate objectsJSObject
- [ABC132F] Small Products 題解
- Multiple Books多賬薄
- POJ1426-Find The Multiple
- 次短路
- 最短路
- 最大值(最短路+最短路計數)
- CF1213E Two Small Strings 細節
- Lowest Common Multiple Plus hd 2028
- 2024_4_22 路徑花費為最長$k$條邊之和最短路
- 最短路-Floyd
- NVDLA-Small在ZCU102上實現
- Netty原始碼—六、tiny、small記憶體分配Netty原始碼記憶體
- HDU 6298 Maximum Multiple(找規律)
- sim3d multiple component example3D
- JavaScript短路表示式JavaScript
- 6.4.2最短路徑
- 最短路專項
- 擴點最短路
- 最短路之Dijkstra
- hdu 4370(最短路)
- 最短路圖論圖論
- 圖 - 最短路徑
- 單源最短路徑:最短路徑性質的證明