結果

問題 No.2128 Round up!!
ユーザー AngrySadEightAngrySadEight
提出日時 2022-10-31 00:39:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 1,744 bytes
コンパイル時間 752 ms
コンパイル使用メモリ 71,716 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 08:42:18
合計ジャッジ時間 3,478 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 93 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 107 ms
4,348 KB
testcase_04 AC 117 ms
4,348 KB
testcase_05 AC 147 ms
4,348 KB
testcase_06 AC 239 ms
4,348 KB
testcase_07 AC 188 ms
4,348 KB
testcase_08 AC 201 ms
4,348 KB
testcase_09 AC 106 ms
4,348 KB
testcase_10 AC 164 ms
4,348 KB
testcase_11 AC 165 ms
4,348 KB
testcase_12 AC 184 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

ll my_gcd(ll x, ll y){
    ll ret;
    if (x < y){
        swap(x, y);
    }
    if (y == 0){
        ret = x;
    }
    else{
        ret = my_gcd(y, x % y);
    }
    return ret;
}

int main(){
    ll mod = 998244353;
    
    int T;
    cin >> T;
    while(T--){
        ll X, A, B;
        cin >> X >> A >> B;
        if (A > B) swap(A, B);

        if (A == B){
            if (X % B == 0) cout << 1 << endl;
            else cout << 2 << endl;
        }
        else{
            //最後に行った操作を操作Bにそろえてから考える

            ll XA = ((X + A - 1) / A) * A;
            ll XB = ((X + B - 1) / B) * B;
            ll XAB = ((XA + B - 1) / B) * B;
            //cout << XA << " " << XB << " " << XAB << endl;

            ll cnt;

            if (XA == XB){
                if (X == XA) cout << 1 << endl;
                else cout << 2 << endl;
                continue;
            }
            else if (XA < XB){
                // X -> XB -> XA -> XAB
                if (X == XA) cnt = 2;
                else cnt = 3;
                X = XAB;
            }
            else{
                // X -> XB
                if (X == XB) cnt = 1;
                else cnt = 2;
                X = XB;
            }
            if (B % A == 0){
                cout << cnt << endl;
                continue;
            }
            ll g = my_gcd(A, B);
            //あと何回操作を行えるか?
            ll lim = A / g;
            ll now = X / B;
            now %= lim;
            if (now == 0) now = lim;
            ll ans = (lim - now) * 2 + cnt;
            cout << ans % mod << endl;
        }
    }
}
0