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 memo={} def modinv(a, m): if (a,m) in memo:return memo[(a,m)] g, x, y = xgcd(a, m) if g != 1: raise Exception('modular inverse does not exist') else: memo[(a,m)]=x%m 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