题意:
初始有一个数
1
1
1,你每次可以将其
∗
10
*10
∗10或者
+
(
x
−
1
)
+(x-1)
+(x−1),现在给你
x
x
x,问最少经过多少步能到达
n
n
n。
1
≤
n
≤
1
e
6
,
1
≤
m
≤
1
e
9
1\le n\le1e6,1\le m\le1e9
1≤n≤1e6,1≤m≤1e9
思路:
同余最短路,定义
d
i
s
[
i
]
dis[i]
dis[i]代表通过两个操作能到的数
m
o
d
n
\bmod n
modn后为
i
i
i的最少操作次数,那么答案就是
d
i
s
[
0
]
dis[0]
dis[0]了,直接扩展就好啦。
最后注意一下
n
=
1
n=1
n=1的情况,别被坑了。
// Problem: Yuki with emofunc and playf
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/18196/B
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,x;
int dis[N];
bool vis[N];
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
cin>>n>>x;
if(n==1) {
puts("0");
return 0;
}
x--;
queue<int>q; q.push(1);
dis[1]=0; vis[1]=true;
x%=n;
while(q.size()) {
int u=q.front(); q.pop();
int now=1ll*u*10%n;
if(!vis[now]) {
vis[now]=1; q.push(now);
dis[now]=dis[u]+1;
}
now=(1ll*u+x)%n;
if(!vis[now]) {
vis[now]=1; q.push(now);
dis[now]=dis[u]+1;
}
}
if(!vis[0]) puts("-1");
else printf("%d\n",dis[0]);
return 0;
}
/*
*/