提出 #493719


ソースコード 拡げる

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define eb emplace_back

#define endl '\n'

typedef long long ll;
typedef pair<int, int> PI;


// ax + by == 1
ll extgcd(ll a, ll b, ll &x, ll &y){
  ll d = a;
  if(b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}


void solve(){
  ll a, b, c;
  cin >> a >> b >> c;
  if(a == c || b == c){
    cout << 1 << endl;
    return;
  }
  
  
  if(c > a && c > b){
    cout << -1 << endl;
    return;
  }

  ll g = __gcd(a, b);
  if(c % g != 0){
    cout << -1 << endl;
    return;
  }
  a /= g;
  b /= g;
  c /= g;
  ll x, y;
  ll ans = 1LL << 60LL;

  rep(i, 2){
    swap(a, b);
    // if(c > a) continue;
    rep(j, 2){
      if(j && a < c) continue;
      if(!j && b < c) continue;
      extgcd(a, b, x, y);
      y = -y;
      // assert(a * x - b * y == 1);
      // cout << i << " " << j << " " << a << " " << b << endl;
      // cout << x << " " << y << endl;
      if(j){
        x *= c - a;
        y *= c - a;
      }else{
        x *= c;
        y *= c;
      }
      
      // seiji

      rep(i, 30){
        if(x < 0 || y < 0){
          x += b << (ll)i;
          y += a << (ll)i;
        }
      }
      
      for(ll i = 30; i >= 0; --i){
        if(x - (b << i) >= 0 && y - (a << i) >= 0){
          x -= b << i;
          y -= a << i;
        }
      }
      
      
      //cout << "aft sub " << x << " " << y << endl;
      ans = min(ans, x * 2 + y * 2);
      // cout << "ans " << ans << endl;
    }
  }
  
  cout << ans << endl;
  // exit(0);
}

int main(int argc, char *argv[])
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  
  int t;
  cin >> t;
  rep(i, t) solve();
  
  return 0;
}

提出情報

提出日時
問題 E - Exact Number of Drops
ユーザ mmnegainoido
言語 C++11 (GCC 4.8.1)
得点 1
コード長 2021 Byte
結果 AC
実行時間 405 ms
メモリ 932 KiB

ジャッジ結果

セット名 All
得点 / 配点 1 / 1
結果
AC × 8
セット名 テストケース
All 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt
ケース名 結果 実行時間 メモリ
001.txt AC 26 ms 728 KiB
002.txt AC 26 ms 920 KiB
003.txt AC 113 ms 808 KiB
004.txt AC 405 ms 728 KiB
005.txt AC 257 ms 920 KiB
006.txt AC 205 ms 916 KiB
007.txt AC 384 ms 932 KiB
008.txt AC 224 ms 928 KiB