中文题意:一个魔术大师需要通过手里的第一张牌的点数通过与别人提供的点数相乘看是否能够得到目标点数。
原文链接:https://www.hackerearth.com/zh/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/dhoom-4/description/
思路;把魔术师第一张牌看成是广度优先搜索的第一个节点置为0,未访问过的节点通通置为-1,目标节点是魔术师第二张牌。
ac代码:
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define all(a) a.begin(),a.end()
#define bitcnt(x) __builtin_popcountll(x)
#define MOD 1000000007
#define total 5000005
#define Me 1000000000001
#define NIL 0
#define MAXN 210005
#define EPS 1e-5
#define INF (1<<28)
#define pi 3.141593
int main()
{
int owkey,reqkey;
int n;
cin>>owkey>>reqkey;
cin>>n;
int a[1005];
int ans[100005];
memset(ans,-1,sizeof(ans));
for(int i=0;i<n;++i)
cin>>a[i];
queue<int >q;
q.push(owkey);
ans[owkey]=0;
while(!q.empty())
{
long long val=q.front();
q.pop();
if(val==reqkey)break;
for(int i=0;i<n;++i)
{
long long to=a[i];
to=to*val;
to%=100000;
if(ans[to]==-1){
ans[to]=ans[val]+1;
q.push(to);
}
}
}
printf("%d\n",ans[reqkey]);
return 0;
}