bfs
直接搜就好了
一开始还以为会爆 unsigned long long,在接近边界的时候还特判了一下
结果发现是小学数学没学好
#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef unsigned long long ll; #define endl '\n' #define pii pair const ll maxn = 110; const ll inf = 1e17 + 10; mapvis; ll n, m, nn = 1; ll cnt[11]; int bfs(ll s) { queueq; q.push(make_pair(s, 0)); while(q.size()) { pii now = q.front(); q.pop(); if(now.first == 0) return now.second; if(vis[now.first]) continue; vis[now.first] = now.second; ll t = now.first, len = 0; while(t) { cnt[t % 10] = 1; t /= 10; len++; } if(len >= n) return now.second; for(int i=2; i<=9; i++) if(cnt[i]) q.push(make_pair(now.first * i, now.second + 1)); for(int i=0; i<=9; i++) cnt[i] = 0; } return -1; } int main() { // ios::sync_with_stdio(false); // cin.tie(0); // cout.tie(0); cin >> n >> m; for(int i=0; i