Loading

CodeForces-1681D Required Length

Required Length

bfs

直接搜就好了

一开始还以为会爆 unsigned long long,在接近边界的时候还特判了一下

结果发现是小学数学没学好

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef unsigned long long ll;
#define endl '\n'
#define pii pair<ll, ll>
const ll maxn = 110;
const ll inf = 1e17 + 10;
map<ll, int>vis;
ll n, m, nn = 1;
ll cnt[11];

int bfs(ll s)
{
    queue<pii>q;
    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<n; i++) nn *= 10;
    cout << bfs(m) << endl;
    return 0;
}
posted @ 2022-05-24 09:55  dgsvygd  阅读(182)  评论(0编辑  收藏  举报