51nod 1109 01组成的N的倍数 bfs+巧妙剪枝+递推式推导

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37428263/article/details/88767741

点击打开链接

//bfs+利用同余原理剪枝+递推关系
#include<bits/stdc++.h>
using namespace std;
typedef pair<string,int>PII;
string solve;
int num=1;
const int maxn=1e9+5;
bool done[maxn];
string bfs(int n)
{
    queue<PII>pq;
    while(!pq.empty()) pq.pop();
    memset(done,false,sizeof(done));
    pq.push(PII("1",1));
    while(!pq.empty())
    {
        PII k=pq.front();
        pq.pop();
        if(done[k.second]) continue;
        done[k.second]=true;
        if(k.second==0) return k.first;
        pq.push( PII ( k.first+"0" , ( k.second* (10%n) )%n ) );
        pq.push( PII ( k.first+"1" , ( ( k.second* (10%n) )%n + 1 )%n ) );
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    if(n==1) printf("1\n");
    else cout<<bfs(n)<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37428263/article/details/88767741