結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー umezoumezo
提出日時 2021-01-23 00:19:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,010 bytes
コンパイル時間 1,962 ms
コンパイル使用メモリ 199,556 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 09:24:06
合計ジャッジ時間 16,423 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 26 ms
4,376 KB
testcase_07 AC 18 ms
4,376 KB
testcase_08 AC 19 ms
4,376 KB
testcase_09 AC 17 ms
4,380 KB
testcase_10 AC 15 ms
4,376 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 AC 905 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const int MOD=1e9+7;

ll extGCD(ll a,ll b,ll &x,ll &y){
  if(b==0){
    x=1;
    y=0;
    return a;
  }
  ll d=extGCD(b,a%b,y,x);
  y-=a/b*x;
  return d;
}

int main(){
  int t;
  cin>>t;
  
  while(t--){
    ll n,k,h,y;
    cin>>n>>k>>h>>y;
    
    if(n>h) swap(n,h);
    if(k>h) swap(k,h);
    if(n>k) swap(n,k);
    
    ll ans=0;
    for(int i=0;i<=y/h;i++){
      if((y-h*i)%gcd(n,k)!=0) continue;
      ll r=(y-h*i)/gcd(n,k);
      ll a=n/gcd(n,k);
      ll b=k/gcd(n,k);    
      
      ll p,q;
      if(extGCD(a,b,p,q)<0) p=-p,q=-q;
      p*=r,q*=r;
      if(q/a<-p/b) continue;
      if(p<=0){
        ans=(ans+q/a+p/b)%MOD;
        if(p%b==0) ans++;
      }
      else if(q<=0){
        ans=(ans+q/a+p/b)%MOD;
        if(q%a==0) ans++;
      }
      else{
        ans=(ans+q/a+p/b+1)%MOD;
      }
    }
    cout<<ans<<endl;
    

  }
  
  return 0;
}
0