結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-13 20:26:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,819 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 87,256 KB
実行使用メモリ 81,416 KB
最終ジャッジ日時 2023-09-28 05:49:51
合計ジャッジ時間 5,598 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
76,440 KB
testcase_01 AC 67 ms
75,544 KB
testcase_02 AC 66 ms
75,480 KB
testcase_03 AC 64 ms
71,284 KB
testcase_04 AC 69 ms
75,304 KB
testcase_05 AC 66 ms
75,524 KB
testcase_06 AC 120 ms
76,784 KB
testcase_07 AC 111 ms
76,772 KB
testcase_08 AC 116 ms
76,828 KB
testcase_09 AC 110 ms
76,928 KB
testcase_10 AC 105 ms
76,812 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #


def main0(n,k,h,y):
  a,b=0,0
  ans=0
  while a<=y:
    b=0
    while a+b<=y:
      yy=y-a-b
      if yy%h==0:
        ans+=1
      b+=k
    a+=n
  return ans
mod=10**9+7
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 a, x0, y0

def modinv(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m
def main1(n,k,h,y):
  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)
  return ans
if __name__=='__main__':
  t=int(input())
  cases=[list(map(int,input().split())) for _ in range(t)]
  for n,k,h,y in cases:
    print(main1(n,k,h,y))

if __name__=='__main__1':
  from random import randint
  for i in range(100):
    ary=[randint(1,10) for _ in range(3)]
    y=randint(10,20)
    n,k,h=ary
    ret0=main0(n,k,h,y)
    ret1=main1(n,k,h,y)
    if ret0!=ret1:
      print(1)
      print(n,k,h,y)
      break
0