题面:题面
思路:bfs,对n取模,并标记减少内存消耗
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,x;
bool vis[1000500];
int main(){
scanf("%lld%lld",&n,&x);
if(n==1)
printf("0\n");
else
{
queue<pair<ll,ll> > q;
q.push(pair<ll,ll>(1,0));
vis[1]=true;
while(!q.empty())
{
pair<ll,ll> p=q.front();
q.pop();
if(p.first%n==0)
{
printf("%lld\n",p.second);
return 0;
}
if(!vis[(p.first+x-1)%n])
{
vis[(p.first+x-1)%n]=true;
q.push(pair<ll,ll> ((p.first+x-1)%n,p.second+1));
}
if(!vis[(p.first*10)%n])
{
vis[(p.first*10)%n]=true;
q.push(pair<ll,ll> ((p.first*10)%n,p.second+1));
}
}
printf("-1\n");
}
return 0;
}