結果

問題 No.2104 Multiply-Add
ユーザー hourenhouren
提出日時 2022-10-21 22:41:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,056 bytes
コンパイル時間 2,029 ms
コンパイル使用メモリ 170,880 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 22:55:57
合計ジャッジ時間 3,682 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:72:14: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   72 |     for(auto [p,q]:ans1){
      |              ^
main.cpp:77:14: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   77 |     for(auto [p,q]:ans2){
      |              ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
#define fix(x) fixed << setprecision(x)
#define asc(x) x, vector<x>, greater<x>
#define rep(i, n) for(ll i = 0; i < n; i++)
#define all(x) (x).begin(),(x).end()
template<class T>bool chmin(T&a, const T&b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool chmax(T&a, const T&b){if(a<b){a=b;return 1;}return 0;}
vector<P> f(int a, int b){
    vector<P> res;
    if(!a){
        res.push_back({1,1});
        a = b;
    }
    if(!b){
        res.push_back({2,1});
        b = a;
    }
    if(a<0){
        int x = (int)2e8 / b;
        res.push_back({1,x});
        a += b*x;
    }
    if(b<0){
        int x = (int)5e8 / a;
        res.push_back({2,x});
        b += a*x;
    }
    while(a!=b){
        if(a>b){
            if(a%b){
                int x = a/b;
                res.push_back({1,-x});
                a %= b;
            }else{
                int x = a/b-1;
                res.push_back({1,-x});
                a = b;
            }
        }else{
            if(b%a){
                int x = b/a;
                res.push_back({2,-x});
                b %= a;
            }else{
                int x = b/a-1;
                res.push_back({2,-x});
                b = a;
            }
        }
    }
    return res;
}
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    if(a==c && b==d){
        cout << "0\n";
        return 0;
    }else if((!a&&!b) || (abs(__gcd(c,d))!=abs(__gcd(a,b)))){
        cout << "-1\n";
        return 0;
    }
    auto ans1 = f(a,b);
    auto ans2 = f(c,d);
    cout << ans1.size()+ans2.size() << '\n';
    reverse(all(ans2));
    for(auto [p,q]:ans1){
        cout << p << " " << q << '\n';
        if(p==1) a += b * q;
        else b += a * q;
    }
    for(auto [p,q]:ans2){
        q *= -1;
        cout << p << " " << q << '\n';
        if(p==1) a += b * q;
        else b += a * q;
    }
    //cout << a << " " << b << '\n';
    return 0;
}
0