結果

問題 No.438 Cwwプログラミング入門
ユーザー cutmdocutmdo
提出日時 2022-09-14 04:04:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,939 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 80,348 KB
実行使用メモリ 814,348 KB
最終ジャッジ日時 2023-08-21 04:06:50
合計ジャッジ時間 7,169 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define PROBLEM "https://yukicoder.me/problems/no/438"

#include <iostream>
#include <numeric>
#include <string>


#include <tuple>

class EuclideanAlgorithm {
    using T = long long;

    // 大きすぎるとオーバーフローしてしまう
    const static inline T m_mx = 1e9;

    const T m_a;
    const T m_b;
    const T m_c;

    T m_gcd;
    T m_x;
    T m_y;

    auto excludedEuclidAlgorithm(T a, T b) -> std::tuple<T, T, T> {
        if(a < 0) {
            auto [g, x, y] = excludedEuclidAlgorithm(-a, -b);
            return {g,-x,-y};
        }
        if(b == 0) { return {a, 1, 0}; }
        auto [g, y, x] = excludedEuclidAlgorithm(b, a % b);
        y -= a / b * x;
        return {g, x, y};
    }

    auto kRange(T x, T b, T l) const -> std::pair<T, T> {
        // x + b * k >= l を満たす k の範囲を求める
        T xd = (l - x);
        if(b == 0 && x >= l) { return {-m_mx,m_mx}; }
        if(b == 0 && x < l) { return {m_mx,-m_mx}; }
        if(b > 0 && xd < 0) { return {xd / b,m_mx}; }
        if(b > 0 && xd >= 0) { return {(xd + b - 1) / b,m_mx}; }
        if(b < 0 && xd < 0) { return {-m_mx,(-xd) / (-b)}; }
        if(b < 0 && xd >= 0) { return {-m_mx,-(xd - b - 1) / (-b)}; }
        return {m_mx,-m_mx};
    }

public:
    auto debug()const {
        std::cout << m_a << " * " << m_x
            << " + " << m_b << " * " << m_y
            << " = " << m_c << std::endl;
        std::cout << "calc: " << m_a * m_x + m_b * m_y
            << " = " << m_c << std::endl;
    }

    EuclideanAlgorithm(T a, T b, T c) :m_a(a), m_b(b), m_c(c) {
        if(a == 0 && b == 0) { throw std::runtime_error(""); }
        auto [g, x, y] = excludedEuclidAlgorithm(a, b);
        if(c % g > 0) { throw std::runtime_error("There is no solution to the equation. c must be divisible by gcd(a,b)."); }
        m_gcd = g; m_x = c / g * x; m_y = c / g * y;
    }
    EuclideanAlgorithm(T a, T b) :EuclideanAlgorithm(a, b, std::gcd(a, b)) {}

    auto gcd() const {
        return m_gcd;
    }
    auto get(T x, T y) const {
        return m_a * x + m_b * y;
    }
    auto get(T k) const ->std::pair<T, T> {
        if(m_b == 0) { return {m_x,m_y - k}; }
        if(m_a == 0) { return {m_x + k,m_y}; }
        return {m_x + m_b * k, m_y - m_a * k};
    }
    auto getMinX(T x_l = 0)const -> std::pair<T, T> {
        return kRange(m_x, m_b, x_l);
    }
    auto getMinY(T y_l = 0)const -> std::pair<T, T> {
        return kRange(m_y, -1 * m_a, y_l);
    }
    auto getMin(T x_l = 0, T y_l = 0)const -> std::pair<T, T> {
        auto [xl, xr] = getMinX(x_l);
        auto [yl, yr] = getMinY(y_l);
        return {std::max(xl,yl),std::min(xr,yr)};
    }
};

using ll = long long;
using std::cout;
using std::cin;
constexpr char endl = '\n';
struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; }_Preprocessing;

auto conv(ll a, ll b, const std::string& ac = "c", const std::string& bc = "w") ->std::string {
    if(2 * (std::abs(a) + std::abs(b)) - 1 > 10000) { return "NO"; }
    if(a <= 0) { return conv(b, a, bc, ac); }
    std::string ret = ac;
    for(int _ = 1; _ < a; ++_) { ret += ac + "C"; }
    for(int _ = 0; _ < b; ++_) { ret += bc + "C"; }
    for(int _ = 0; _ < -b; ++_) { ret += bc + "W"; }
    return ret;
}

signed main() {
    ll x, y, z;
    cin >> x >> y >> z;

    if(z % std::gcd(x, y) > 0) { cout << "NO" << endl; return 0; }

    auto ea = EuclideanAlgorithm(x, y, z);
    auto [kl, kr] = ea.getMin();

    ll ka = 0, kb = 0, sum = 1e18;
    for(int k = kl - 10; k <= kl + 10; ++k) {
        auto [a, b] = ea.get(k);
        auto s = std::abs(a) + std::abs(b);
        if(s < sum) { sum = s; ka = a; kb = b; }
    }
    for(int k = kr - 10; k <= kr + 10; ++k) {
        auto [a, b] = ea.get(k);
        auto s = std::abs(a) + std::abs(b);
        if(s < sum) { sum = s; ka = a; kb = b; }
    }

    auto ans = conv(ka, kb);
    cout << ans << endl;
}
0