結果

問題 No.826 連絡網
ユーザー MisterMister
提出日時 2020-04-27 19:00:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,868 bytes
コンパイル時間 1,006 ms
コンパイル使用メモリ 88,376 KB
実行使用メモリ 368,852 KB
最終ジャッジ日時 2024-05-01 21:06:20
合計ジャッジ時間 12,948 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 6 ms
5,376 KB
testcase_04 AC 7 ms
5,888 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 6 ms
5,632 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 7 ms
5,732 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 6 ms
5,376 KB
testcase_12 TLE -
testcase_13 AC 675 ms
118,848 KB
testcase_14 AC 1,561 ms
231,296 KB
testcase_15 AC 85 ms
25,600 KB
testcase_16 AC 916 ms
151,552 KB
testcase_17 AC 707 ms
119,168 KB
testcase_18 AC 491 ms
91,896 KB
testcase_19 TLE -
testcase_20 TLE -
testcase_21 AC 9 ms
6,944 KB
testcase_22 AC 674 ms
121,068 KB
testcase_23 AC 912 ms
157,272 KB
testcase_24 AC 290 ms
66,208 KB
testcase_25 TLE -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }

    int size() const { return graph.size(); }
};

template <class Cost>
std::vector<int> bfs(const Graph<Cost>& graph, int s) {
    std::vector<Cost> dist(graph.size(), -1);
    dist[s] = 0;
    std::queue<int> que;
    que.push(s);

    while (!que.empty()) {
        int v = que.front();
        que.pop();

        for (const auto& e : graph[v]) {
            if (dist[e.dst] != -1) continue;
            dist[e.dst] = dist[v] + e.cost;
            que.push(e.dst);
        }
    }

    return dist;
}

void solve() {
    int n, p;
    std::cin >> n >> p;

    Graph<> graph(n + 1);
    for (int d = 2; d <= n; ++d) {
        for (int i = d; i + d <= n; i += d) {
            graph.span(false, i, i + d);
        }
    }

    auto ds = bfs(graph, p);
    std::cout << std::count_if(ds.begin(), ds.end(),
                               [](int d) { return d != -1; })
              << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0