結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー どららどらら
提出日時 2021-01-23 00:25:40
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 670 ms / 2,000 ms
コード長 1,292 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 170,936 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 09:57:38
合計ジャッジ時間 5,913 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 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,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 579 ms
4,380 KB
testcase_12 AC 524 ms
4,380 KB
testcase_13 AC 670 ms
4,376 KB
testcase_14 AC 456 ms
4,376 KB
testcase_15 AC 512 ms
4,376 KB
testcase_16 AC 646 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;
}

ll calc(ll a, ll b, ll c, ll g, ll x, ll y){

  if(c%g != 0) return 0;
  a /= g;
  b /= g;
  c /= g;
  
  ll sp = c * x;
  ll sq = c * y;

  ll n = sq/a;
  if(sq%a<0) n--;
  ll m = (-sp + b-1)/b;
  if((-sp + b-1)%b<0) m--;
  //cout << c << " " << n << " " << m << endl;
  return max(0LL, n-m+1);
}


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 g = gcd(N, K);
  ll d = extgcd(K, N, x, y);
  
  ll ret = 0;
  for(int z=0; z<=Y/H; z++){
    ll a = K;
    ll b = N;
    ll c = Y-z*H;
    
    ret += calc(a, b, c, g, x, y);
    ret %= MOD;
  }
  cout << ret << endl;
}


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

0