結果

問題 No.1315 渦巻洞穴
ユーザー PachicobuePachicobue
提出日時 2020-12-12 01:49:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 7,673 bytes
コンパイル時間 2,006 ms
コンパイル使用メモリ 211,768 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-19 21:41:08
合計ジャッジ時間 4,842 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 3 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 4 ms
6,944 KB
testcase_19 AC 4 ms
6,940 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 AC 4 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 3 ms
6,944 KB
testcase_25 AC 4 ms
6,944 KB
testcase_26 AC 4 ms
6,940 KB
testcase_27 AC 4 ms
6,940 KB
testcase_28 AC 4 ms
6,940 KB
testcase_29 AC 4 ms
6,944 KB
testcase_30 AC 4 ms
6,940 KB
testcase_31 AC 4 ms
6,944 KB
testcase_32 AC 4 ms
6,944 KB
testcase_33 AC 4 ms
6,944 KB
testcase_34 AC 4 ms
6,944 KB
testcase_35 AC 4 ms
6,940 KB
testcase_36 AC 4 ms
6,940 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 3 ms
6,940 KB
testcase_39 AC 4 ms
6,940 KB
testcase_40 AC 5 ms
6,940 KB
testcase_41 AC 5 ms
6,944 KB
testcase_42 AC 4 ms
6,944 KB
testcase_43 AC 5 ms
6,940 KB
testcase_44 AC 5 ms
6,944 KB
testcase_45 AC 5 ms
6,940 KB
testcase_46 AC 4 ms
6,940 KB
testcase_47 AC 6 ms
6,940 KB
testcase_48 AC 5 ms
6,944 KB
testcase_49 AC 5 ms
6,940 KB
testcase_50 AC 5 ms
6,940 KB
testcase_51 AC 5 ms
6,944 KB
testcase_52 AC 5 ms
6,944 KB
testcase_53 AC 5 ms
6,940 KB
testcase_54 AC 5 ms
6,944 KB
testcase_55 AC 4 ms
6,940 KB
testcase_56 AC 5 ms
6,944 KB
testcase_57 AC 5 ms
6,940 KB
testcase_58 AC 5 ms
6,940 KB
testcase_59 AC 3 ms
6,940 KB
testcase_60 AC 3 ms
6,940 KB
testcase_61 AC 3 ms
6,944 KB
testcase_62 AC 3 ms
6,944 KB
testcase_63 AC 3 ms
6,944 KB
testcase_64 AC 2 ms
6,944 KB
testcase_65 AC 3 ms
6,940 KB
testcase_66 AC 3 ms
6,940 KB
testcase_67 AC 3 ms
6,944 KB
testcase_68 AC 3 ms
6,940 KB
testcase_69 AC 3 ms
6,940 KB
testcase_70 AC 3 ms
6,944 KB
testcase_71 AC 4 ms
6,940 KB
testcase_72 AC 3 ms
6,940 KB
testcase_73 AC 3 ms
6,944 KB
testcase_74 AC 4 ms
6,940 KB
testcase_75 AC 2 ms
6,940 KB
testcase_76 AC 2 ms
6,940 KB
testcase_77 AC 4 ms
6,940 KB
testcase_78 AC 2 ms
6,940 KB
testcase_79 AC 4 ms
6,940 KB
testcase_80 AC 4 ms
6,940 KB
testcase_81 AC 4 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using ld = long double;
template<typename T> using max_heap = std::priority_queue<T>;
template<typename T> using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; }
constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; }
constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); }
constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; }
constexpr ull ceil2(const ull v) { return 1ULL << clog(v); }
constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; }
constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; }
template<typename T> void bset(T& mask, const int ind) { mask |= ((T)1 << ind); }
template<typename T> void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); }
template<typename T> void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); }
template<typename T> void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); }
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }
template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
template<typename T> constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN<T>(n - 1) * T{10}; }
template<typename F> struct fix : F
{
    fix(F&& f) : F{std::forward<F>(f)} {}
    template<typename... Args> auto operator()(Args&&... args) const { return F::operator()(*this, std::forward<Args>(args)...); }
};
template<typename T, int n, int i = 0>
auto nd_array(int const (&szs)[n], const T x = T{})
{
    if constexpr (i == n) {
        return x;
    } else {
        return std::vector(szs[i], nd_array<T, n, i + 1>(szs, x));
    }
}
class printer
{
public:
    printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); }
    template<typename... Args> int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; }
    template<typename... Args> int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; }
private:
    template<typename T> void dump(const T& v) { m_os << v; }
    template<typename T> void dump(const std::vector<T>& vs)
    {
        for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); }
    }
    template<typename T> void dump(const std::vector<std::vector<T>>& vss)
    {
        for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); }
    }
    template<typename T, typename... Args> int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; }
    std::ostream& m_os;
};
printer out;
class scanner
{
public:
    scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); }
    template<typename T> T val()
    {
        T v;
        return m_is >> v, v;
    }
    template<typename T> T val(const T offset) { return val<T>() - offset; }
    template<typename T> std::vector<T> vec(const int n)
    {
        return make_v<T>(n, [this]() { return val<T>(); });
    }
    template<typename T> std::vector<T> vec(const int n, const T offset)
    {
        return make_v<T>(n, [this, offset]() { return val<T>(offset); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1)
    {
        return make_v<std::vector<T>>(n0, [this, n1]() { return vec<T>(n1); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset)
    {
        return make_v<std::vector<T>>(n0, [this, n1, offset]() { return vec<T>(n1, offset); });
    }
    template<typename... Args> auto tup() { return std::tuple<std::decay_t<Args>...>{val<Args>()...}; }
    template<typename... Args> auto tup(const Args&... offsets) { return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...}; }
private:
    template<typename T, typename F>
    std::vector<T> make_v(const int n, F f)
    {
        std::vector<T> ans;
        for (int i = 0; i < n; i++) { ans.push_back(f()); }
        return ans;
    }
    std::istream& m_is;
};
scanner in;
int main()
{
    using pll = std::pair<ll, ll>;
    auto pos = [&](const ll N) -> pll {
        ll i = 0;
        for (;; i++) {
            if (N <= (2LL * i + 1) * (2LL * i + 1)) { break; }
        }
        ll end = (2LL * i + 1) * (2LL * i + 1);
        ll y = i, x = i;
        if (end - (2 * i) <= N) { return {y, x - (end - N)}; }
        end -= 2 * i, x -= 2 * i;
        if (end - (2 * i) <= N) { return {y - (end - N), x}; }
        end -= 2 * i, y -= 2 * i;
        if (end - (2 * i) <= N) { return {y, x + (end - N)}; }
        end -= 2 * i, x += 2 * i;
        return {y + (end - N), x};
    };
    auto val = [&](const ll y, const ll x) -> ll {
        ll i = std::max(std::abs(x), std::abs(y));
        ll end = (2LL * i + 1) * (2LL * i + 1);
        ll ey = i, ex = i;
        if (y == i) { return end - (ex - x); }
        end -= 2 * i, ex -= 2 * i;
        if (x == -i) { return end - (ey - y); }
        end -= 2 * i, ey -= 2 * i;
        if (y == -i) { return end - (x - ex); }
        end -= 2 * i, ex += 2 * i;
        return end - (y - ey);
    };
    const auto [S, T] = in.tup<ll, ll>();
    const auto [sy, sx] = pos(S);
    const auto [ty, tx] = pos(T);
    static_cast<void>(0);
    auto route = [&](ll y, ll x, const ll ty, const ll tx) {
        std::string ans;
        ll cnt = 0;
        while (y < ty) { ans += (cnt == 0 ? "DUD" : "D"), y++, cnt ^= 1; }
        while (y > ty) { ans += (cnt == 0 ? "UDU" : "U"), y--, cnt ^= 1; }
        while (x < tx) { ans += (cnt == 0 ? "RLR" : "R"), x++, cnt ^= 1; }
        while (x > tx) { ans += (cnt == 0 ? "LRL" : "L"), x--, cnt ^= 1; }
        return ans;
    };
    auto reverse = [&](const std::string& s) {
        std::string rs = s;
        std::reverse(rs.begin(), rs.end());
        for (auto& c : rs) {
            if (c == 'U') {
                c = 'D';
            } else if (c == 'D') {
                c = 'U';
            } else if (c == 'L') {
                c = 'R';
            } else if (c == 'R') {
                c = 'L';
            }
        }
        return rs;
    };
    std::string ans;
    if ((sy + sx + ty + tx) % 2 != 0) {
        ans = route(sy, sx, ty, tx);
        out.ln(0);
        out.ln(ans.size());
        out.ln(ans);
    } else {
        if (std::abs(sy + sx) % 2 == 0) {
            ans += route(sy, sx, 0, 0);
            ans += "RU";
            ans += reverse(route(ty, tx, -1, 1));
        } else {
            ans += route(sy, sx, 0, 0);
            ans += "RLRUDL";
            ans += reverse(route(ty, tx, 0, 0));
        }
        out.ln(0);
        out.ln(ans.size());
        out.ln(ans);
    }
    auto debug = [&]() {
        ll y = sy, x = sx;
        ll X = val(sy, sx);
        std::vector<ll> xs{X};
        for (const char c : ans) {
            if (c == 'U') {
                y--;
            } else if (c == 'D') {
                y++;
            } else if (c == 'L') {
                x--;
            } else {
                x++;
            }
            xs.push_back(val(y, x));
            X ^= val(y, x);
        }
        static_cast<void>(0);
        assert(X == 0);
        assert(x == tx and y == ty);
    };
    debug();
    return 0;
}
0