結果

問題 No.2104 Multiply-Add
ユーザー 👑 NachiaNachia
提出日時 2022-10-21 21:51:04
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,232 bytes
コンパイル時間 880 ms
コンパイル使用メモリ 82,544 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-01 06:27:05
合計ジャッジ時間 2,276 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <atcoder/modint>

using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(int)(n); i++)

i64 GCD(i64 a, i64 b){
    a = abs(a);
    b = abs(b);
    if(a == 0 || b == 0) return a + b;
    while(b){
        a %= b;
        swap(a, b);
    }
    return a;
}

const i64 INF = 1001001001001001001;
using Modint = atcoder::static_modint<998244353>;

vector<pair<int, i64>> Query(i64 a, i64 b){
    i64 g = GCD(a,b);
    a /= g; b /= g;
    vector<pair<int, i64>> res;
    auto query = [&](int t, i64 k){
        res.push_back({t,k});
        if(t == 1) a += b * k;
        else b += a * k;
    };
    if(a < 0 && b < 0){
        if(a < b) query(2, -1); else query(1, -1);
    }
    if(a < 0 && b > 0){
        query(1, abs(a)/b+1);
    }
    if(a > 0 && b < 0){
        query(2, abs(b)/a+1);
    }
    if(a < 0 && b == 0){
        query(2, 1);
        query(1, -1);
    }
    if(a == 0 && b < 0){
        query(1, -1);
        query(2, 1);
    }
    while(a != 0 && b != 0){
        if(a > b) query(1, -(a/b));
        else query(2, -(b/a));
    }
    if(a != 0){
        query(2, 1);
        query(1, -1);
    }
    //cout << "state = [ " << a << " , " << b << " ]" << endl;
    return res;
}

int main(){
    i64 a,b,c,d; cin >> a >> b >> c >> d;

    if((a == 0 && b == 0) || (c == 0 && d == 0)){
        if((a == 0 && b == 0) && (c == 0 && d == 0)){
            cout << "0\n";
        }
        else{
            cout << "-1\n";
        }
        return 0;
    }

    if(GCD(c,d) - GCD(a,b) != 0){
        cout << "-1\n";
        return 0;
    }

    auto P1 = Query(a, b);
    auto P2 = Query(c, d);

    reverse(P2.begin(), P2.end());
    for(auto& p : P2) p.second *= -1;
    
    auto N = P1.size() + P2.size();
    cout << N << "\n";
    for(auto& p : P1) cout << p.first << " " << p.second << "\n";
    for(auto& p : P2) cout << p.first << " " << p.second << "\n";
    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;


0