結果

問題 No.2767 Add to Divide
ユーザー ypwwypww
提出日時 2024-05-31 23:23:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 2,536 bytes
コンパイル時間 4,567 ms
コンパイル使用メモリ 267,188 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-31 23:23:48
合計ジャッジ時間 6,012 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 31 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 20 ms
6,944 KB
testcase_06 AC 19 ms
6,944 KB
testcase_07 AC 21 ms
6,944 KB
testcase_08 AC 26 ms
6,940 KB
testcase_09 AC 25 ms
6,944 KB
testcase_10 AC 25 ms
6,940 KB
testcase_11 AC 26 ms
6,944 KB
testcase_12 AC 25 ms
6,944 KB
testcase_13 AC 24 ms
6,940 KB
testcase_14 AC 24 ms
6,940 KB
testcase_15 AC 23 ms
6,944 KB
testcase_16 AC 25 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;

#define rep(i, a, b) for(ll i=a; i<b; i++)
#define rrep(i, a, b) for(ll i=a; i>=b; i--)
#define all(a) (a).begin(), (a).end()
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

template<typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template<typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }

inline int popcount(int n) { return __builtin_popcount(n); } // 2進数で表した場合に立ってるビット数がいくつか
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int ctz(int n) { return n != 0 ? __builtin_ctz(n) : -1; } // 2進数で表した場合に 1 の位からいくつ 0 が連なっているか
inline int ctz(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int clz(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; } // 2進数で表した場合に左側にいくつ 0 を埋める必要があるか
inline int clz(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }

const double PI = 3.141592653589793;
const vector<int> DX = { 1, 0, -1, 0 };
const vector<int> DY = { 0, 1, 0, -1 };
const long long INF = 4004004003104004004LL; // (int)INF = 1010931620;
ll coordinate(ll h, ll w, ll W){ return h*W + w; } // 二次元座標を一次元座標に変換

#define endl "\n" // インタラクティブの時はコメントアウトする

vector<long long> divisor(long long n){
    vector<long long> res;
    for (long long i = 1; i * i <= n; i++){
        if (n % i == 0) {
            res.push_back(i);
            if (i * i != n) res.push_back(n / i);
        }
    }
    sort(res.begin(), res.end());
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    // cout << fixed << setprecision(18);

    ll t;
    cin>>t;
    while(t--){
        ll a,b;
        cin>>a>>b;
        if (a==b){
            cout << 0 << endl;
            continue;
        }
        ll ans = INF;
        auto arr = divisor(b-a);
        for (auto y: arr){
            ll temp = (b-a)/y - a;
            if (temp<0) continue;
            chmin(ans, temp);
        }
        if (ans==INF) ans = -1;
        cout << ans << endl;
    }

    
    return 0;
}
0