結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー どららどらら
提出日時 2021-01-23 02:03:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 744 ms / 2,000 ms
コード長 1,665 bytes
コンパイル時間 1,608 ms
コンパイル使用メモリ 171,124 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 12:13:12
合計ジャッジ時間 6,616 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 9 ms
4,376 KB
testcase_07 AC 7 ms
4,380 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 644 ms
4,376 KB
testcase_12 AC 580 ms
4,380 KB
testcase_13 AC 744 ms
4,380 KB
testcase_14 AC 508 ms
4,376 KB
testcase_15 AC 566 ms
4,376 KB
testcase_16 AC 709 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

static const ll MOD = 1000000007;

ll gcd(ll a, ll b){return (b==0?a:gcd(b,a%b));}
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;
}

// a*X+b*Y==cとなる自然数のペア(X,Y)について、
// X = x*c + b*k, Y = (c - a*X)/bとなるkの範囲[mn,mx]を返す(個数はn-m+1個)
pair<ll,ll> first_order_indeterminate_equation(ll a, ll b, ll c, ll g, ll x, ll y){
  if(c%g != 0) return make_pair(1,0);
  a /= g; b /= g; c /= g;

  ll t = c/a;
  ll n = (t - x * c)/b;
  if((t - x * c) % b < 0) n--;
  ll m = (- x * c + b-1)/b;
  if((- x * c + b-1) % b < 0) m--;

  /*
  cout << a << " " << b << " " << c << endl;
  for(ll k=m; k<=n; k++){
    ll X = x*c+b*k;
    ll Y = (c-a*X)/b;
    cout << "(" << X << " " << Y << ")" << endl;
  }
   */
  
  return make_pair(m, n);
}


void solve(){
  ll N, K, H, Y;
  cin >> N >> K >> H >> Y;

  vector<ll> v{N,K,H};
  sort(ALLOF(v));
  N = v[0];
  K = v[1];
  H = v[2];

  ll x, y;
  ll d = extgcd(N, K, x, y);
  ll g = gcd(N,K);
  
  ll ret = 0;
  for(int z=0; z<=Y/H; z++){
    pair<ll,ll> res = first_order_indeterminate_equation(N, K, Y-z*H, g, x, y);
    ret += res.second - res.first + 1;
    ret %= MOD;
  }
  cout << ret << endl;
}


int main(){
  int t;
  cin >> t;
  rep(i,t){
    solve();
  }
  
  return 0;
}

0