zoukankan      html  css  js  c++  java
  • 51Nod 1109 01组成N的倍数

    给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且M的10进制表示只包含0或1。求最小的M。
     
    例如:N = 4,M = 100。
    Input
    输入1个数N。(1 <= N <= 10^6)
    Output
    输出符合条件的最小的M。
    Input示例
    4
    Output示例
    100

    思路:最开始一直在想构造方法,歪了。想到同余定理就是简单题了,bfs一下,不断末尾添加'0'或'1',保存一下当前余数,如果访问过就跳过。总复杂度大概nlog(n)。log是因为在bfs时向队列传入的字符
    串长度会达到log级别。
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #include <iomanip>
    #include <cctype>
    #include <cassert>
    #include <bitset>
    #include <ctime>
    
    using namespace std;
    
    #define pau system("pause")
    #define ll long long
    #define pii pair<int, int>
    #define pb push_back
    #define mp make_pair
    #define clr(a, x) memset(a, x, sizeof(a))
    
    const double pi = acos(-1.0);
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e9 + 7;
    const double EPS = 1e-9;
    
    struct gg {
        string s;
        int a;
        gg(){}
        gg(string s, int a) : s(s), a(a) {}
    };
    bool vis[1000015];
    queue<gg> que;
    int n;
    int main() {
        scanf("%d", &n);
        if (1 == n) {
            printf("1");
            return 0;
        }
        que.push(gg("1", 1 % n));
        vis[1] = 1;
        while (que.size()) {
            gg g = que.front(); que.pop();
            for (int i = 0; i < 2; ++i) {
                string s = g.s + (char)('0' + i);
                int a = (g.a * 10 + i) % n;
                if (!a) {
                    cout << s;
                    return 0;
                }
                if (vis[a]) continue;
                que.push(gg(s, a)); vis[a] = 1;
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    mount命令详解
    traceroute命令详解
    etcd节点扩容至两个节点
    shell历史命令
    etcd单节点安装
    linux中修改环境变量及生效方法
    ansible最佳实战部署nginx
    用roles部署nginx
    playbook部署mangodb
    安装mangodb
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/7898450.html
Copyright © 2011-2022 走看看