結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-13 20:33:05
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,305 bytes
コンパイル時間 668 ms
コンパイル使用メモリ 87,400 KB
実行使用メモリ 77,376 KB
最終ジャッジ日時 2023-10-08 03:26:54
合計ジャッジ時間 12,538 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
76,480 KB
testcase_01 AC 70 ms
71,140 KB
testcase_02 AC 72 ms
71,584 KB
testcase_03 AC 71 ms
71,356 KB
testcase_04 AC 72 ms
71,544 KB
testcase_05 AC 73 ms
71,192 KB
testcase_06 AC 121 ms
77,084 KB
testcase_07 AC 104 ms
76,604 KB
testcase_08 AC 115 ms
77,252 KB
testcase_09 AC 118 ms
77,260 KB
testcase_10 AC 114 ms
77,224 KB
testcase_11 AC 1,859 ms
77,108 KB
testcase_12 AC 1,659 ms
77,228 KB
testcase_13 TLE -
testcase_14 AC 1,459 ms
76,992 KB
testcase_15 AC 1,701 ms
76,860 KB
testcase_16 AC 602 ms
77,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def gcd(a,b):
  while b:a,b=b,a%b
  return a

def xgcd(a, b):
    x0, y0, x1,y1=1,0,0,1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return x0
memo={}
def modinv(a, m):
    if (a,m) in memo:return memo[(a,m)]
    x=xgcd(a, m)
    memo[(a,m)]=x%m
    return x % m

import sys
input=sys.stdin.readline
t=int(input())
cases=[list(map(int,input().split())) for _ in range(t)]
mod=10**9+7
for n,k,h,y in cases:
  ary=[n,k,h]
  ary.sort()
  a,b,c=ary
  g=gcd(a,b)
  na,nb=a//g,b//g
  now=0
  ans=0
  while now<=y:
    yy=y-now
    now+=c
    if yy%g!=0:continue
    yy=yy//g
    if yy==0:
      ans+=1
      continue
    if yy<na:continue
    if yy<nb:
      if yy%na==0:
        ans+=1
      continue
    # yy-nb*iがmod naで0になるiの個数
    # yy%na=nb*i%na となるiの個数
    # 0<=i<=yy//nb
    # yy%na=nb*i%na となる最小のiを求める。あとは周期naで循環する。
    u=yy%na
    v=nb%na
    # u=v*i
    # u*v^(-1)=i
    if u==0:
      i=0
    elif u==v:
      i=1
    else:
      i=u*modinv(v,na)
      i%=na
    #print((na,nb),yy,i)
    if yy-nb*i<0:continue
    w=yy//nb
    if i<=w:
      ans+=(w-i)//na+1
      ans%=mod
      #print((u,v,w))
      #print((na,nb,yy),i,w,(w-i)//na+1)
  print(ans)
0