POJ-1426-Find The Multiple

链接:https://vjudge.net/problem/POJ-1426

题意:

给定n,求有1和0组成的可以整出n的十进制数。

思路:

裸BFS加STL队列会超时,用余数优化即可

代码:

#include <iostream>
#include <memory.h>
#include <queue>
using namespace std;
typedef long long LL;
int vis[220];

int main()
{
    int n;
    while (cin>>n&&n)
    {
        if (n == 1)
            cout << 1 << endl;
        else
        {
            memset(vis,0,sizeof(vis));
            LL v;
            queue<LL> Q;
            Q.push(1);
            vis[1%n] = 1;
            while (!Q.empty())
            {
                LL x = Q.front();
                Q.pop();
                LL nex = x*10;
                if (nex%n == 0)
                {
                    v = nex;
                    break;
                }
                else if (vis[nex%n] == 0)
                {
                    Q.push(nex);
                    vis[nex%n] = 1;
                }
                nex++;
                if (nex%n == 0)
                {
                    v = nex;
                    break;
                }
                else if (vis[nex%n] == 0)
                {
                    Q.push(nex);
                    vis[nex%n] = 1;
                }
            }
            cout << v << endl;
        }
    }

    return 0;
}

  

原文地址:https://www.cnblogs.com/YDDDD/p/10268625.html