結果

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

テストケース

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

ソースコード

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, char ac = 'c', char 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 = "";
    ret += std::string(std::abs(b), bc);
    ret += std::string(a, ac);
    ret += std::string(a - 1, 'C');
    ret += (b > 0) ? std::string(b, 'C') : std::string(-b, 'W');
    return ret;
}

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

    if(z == 0) { cout << "ccW" << endl; return 0; }
    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