Problem: Takahashi has a tower which is divided into N layers. Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower. He defines the _beauty of the tower_ as follows:    * The beauty of the tower is the sum of the scores of the N layers, where the score of a layer is A if the layer is painted red, A+B if the layer is painted green, B if the layer is painted blue, and 0 if the layer is uncolored.  Here, A and B are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors.  Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly K. How many such ways are there to paint the tower? Find the count modulo 998244353. Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other.

Submission attempts (in order):
n,a,b,k=map(int,input().split())
p=998244353
def getinv(n):
  inv=[0]*(n+1)
  inv[1]=1
  for i in range(2,n+1):
    inv[i]=(-(p//i)*inv[p%i])%p
  return inv
def getnCr(n):
  inv=getinv(n)
  nCr=[0]*(n+1)
  nCr[0]=1
  for i in range(1,n+1):
    nCr[i]=(nCr[i-1]*(n-i+1)*inv[i])%p
  return nCr
def solve(n,a,b,k):
  ans=0
  nCr=getnCr(n)
  for i in range(n+1):
    if (k-a*i)<0 or (k-a*i)%b!=0:
      continue
    j=(k-a*i)//b
    if a*i+b*j==k:
      print(i)
      print(j)
      ans+= nCr[i]*nCr[j]
      ans%=p
  return ans
print(solve(n,a,b,k))

p=998244353
def getinv(n):
  inv = [0]*(n+1)
  
  inv[1] = 1
  for i in range(2,n+1):
    
    inv[i] = (-(p//a)*inv[p%a])%p
  return inv

def getCmb(n):
  
  inv = getinv(n)
  
  nCr = [0]*(n+1)
  
  nCr[0] = 1
  for i in range(1,n+1):
    
    nCr[i] = (nCr[i-1]*(n-i+1)*inv[i])%p
  return nCr

def solve(n,a,b,k):
  nCr = getCmb(n)
  ans=0
  for i in range(n+1):
    
    j = (k-a*i)//b
    
    if a*i + b*j == k and 0<=j<=n:
      ans+= (nCr[i]*nCr[j])%p
      ans%= p
  return ans

n,a,b,k=map(int,input().split())
print(solve(n,a,b,k))

n,a,b,k=map(int,input().split())
p=998244353
def getinv(n):
  inv=[0]*(n+1)
  inv[1]=1
  for i in range(2,n+1):
    inv[i]=(-p//i*inv[p%i])%p
  return inv
def getnCr(n):
  inv=getinv(n)
  nCr=[0]*(n+1)
  nCr[0]=1
  for i in range(1,n+1):
    nCr[i]=(nCr[i-1]*(n-i-1)*inv[i])%p
  return nCr
def solve(n,a,b,k):
  ans=0
  nCr=getnCr(n)
  for i in range(n+1):
    if (k-a*i)%b!=0:
      continue
    j=(k-a*i)//b
    if a*i+b*j==k:
      ans+=(nCr[i]*nCr[j])%p
      ans%=p
  return ans
print(solve(n,a,b,k))

import math
n,a,b,k=map(int,input().split())
def gcd(x,y):
  if y==0:
    return x
  else:
    return gcd(y,x%y)
p=gcd(a,b)
ans=0
for i in range(k//(a+b)):
  if (k-(a+b)*i)%p!=0:
    continue
  else:
    q=gcd(p,k-(a+b)*i)
    A=a//q
    B=b//q
    K=k//q
    A_=(pow(A,B-2,B)*K)%p
    for i in range((n-i-A_)//B):
      ans+=math.factorial(n-i)//(math.factorial(A_+B*i)*math.factorial((k-A_+B*i)//B)*math.factorial(n-i-A_+B*i-(k-A_+B*i)//B))
print(ans)

n,a,b,k=map(int,input().split())
p=998244353
def getinv(n):
  inv=[0]*(n+1)
  inv[1]=1
  for i in range(2,N+1):
    inv[i]=(-p//i*inv[p%i])%p
  return inv
def getnCr(n):
  inv=getinv(n)
  nCr=[0]*(n+1)
  nCr[0]=1
  for i in range(1,n+1):
    nCr[i]=(nCr[i-1]*(n-i-1)*inv[i])%p
  return nCr
def solve(n,a,b,k):
  ans=0
  nCr=getnCr(n)
  for i in range(n+1):
    j=(k-a*i)//b
    if a*i+b*j==k:
      ans+=nCr[i]+nCr[j]
      ans%=p
  return ans
print(solve(n,a,b,k))

from math import factorial
n,a,b,k=map(int,input().split())
def gcd(x,y):
  if y==0:
    return x
  else:
    return gcd(y,x%y)
ans=0
p=998244353
x=gcd(a,b)
if K%x==0:
  A=a//x
  B=b//x
  K=k//x
  for i in range(n):
    if (K-A*i)%B==0:
      ans+= (factorial(n)//factorial(n-i)//factorial(i))*(factorial(n)//factorial(n-(K-A*i)//B)//factorial((K-A*i)//B))
print(ans)
  

p=998244353
def getinv(n):
  inv = [0]*(n+1)
  
  inv[1] = 1
  for i in range(2,n+1):
    
    inv[i] = (-(p//i)*inv[p%i])%p
  return inv

def getCmb(n):
  
  inv = getinv(n)
  
  nCr = [0]*(n+1)
  
  nCr[0] = 1
  for i in range(1,n+1):
    
    nCr[i] = (nCr[i-1]*(n-i+1)*inv[i])%p
  return nCr

def solve(n,a,b,k):
  nCr = getCmb(n)
  ans=0
  for i in range(n+1):
    
    j = (k-a*i)//b
    
    if a*i + b*j == k and 0<=j<=n:
      ans+= (nCr[i]*nCr[j])%p
      ans%= p
  return ans

n,a,b,k=map(int,input().split())
print(solve(n,a,b,k))

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted