結果

問題 No.2996 Floor Sum
ユーザー 👑 p-adicp-adic
提出日時 2024-12-20 11:50:28
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 6,996 bytes
コンパイル時間 913 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 178,916 KB
最終ジャッジ日時 2024-12-21 18:07:05
合計ジャッジ時間 23,121 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
178,916 KB
testcase_01 AC 1,713 ms
96,340 KB
testcase_02 AC 252 ms
78,796 KB
testcase_03 AC 3,951 ms
93,100 KB
testcase_04 AC 1,346 ms
87,940 KB
testcase_05 AC 1,521 ms
93,868 KB
testcase_06 AC 1,680 ms
92,208 KB
testcase_07 AC 361 ms
80,944 KB
testcase_08 AC 997 ms
87,344 KB
testcase_09 AC 666 ms
82,564 KB
testcase_10 AC 561 ms
82,208 KB
testcase_11 AC 277 ms
79,652 KB
testcase_12 AC 701 ms
84,028 KB
testcase_13 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

R=range
class ModB:
	B = 998244353
	length_bound = 10**6 #User definition
	length_max = min( length_bound , B )
	inverse=None
	factorial=None
	factorial_inverse=None
	def SetModulo(B):
		ModB.B = int(B)
		assert(ModB.B > 0)
		ModB.length_max = min( ModB.length_bound , ModB.B )
		ModB.inverse = [None,1 if ModB.B>1 else 0]
		ModB.factorial = [1 if ModB.B>1 else 0]
		ModB.factorial_inverse = [1 if ModB.B>1 else 0]

	def __init__(self,val,valid = False):
		self.val = int(val)
		if not valid and not(0 <= self.val < ModB.B):self.val %= ModB.B
	def ref(n):
		return n if n.__class__ == __class__ else ModB(n,True)
	def get(n):
		return n.val if n.__class__ == __class__ else n
	def copy(self):
		return ModB(self.val,True)

	def __str__(self):
		return str(self.val)

	def __eq__(self,x):
		return x==self.val
	def __ne__(self,other):
		return not( self == other )

	def __iadd__(self,x):
		self.val += ModB.ref(x).val
		if self.val >= ModB.B:self.val -= ModB.B
		return self
	def __add__(self,x):
		a = self.copy()
		a += x
		return a
	def __radd__(self,x):
		return ModB(x + self.val)

	def __neg__(self):
		return ModB(ModB.B - self.val if self.val else 0,True)
	def __isub__(self,x):
		self.val -= ModB.ref(x).val
		if self.val < 0:self.val += ModB.B
		return self
	def __sub__(self,x):
		a = self.copy()
		a -= x
		return a
	def __rsub__(self,x):
		return ModB(x - self.val)

	def __mul__(self,x):
		return ModB.get(x) * self
	def __rmul__(self,x):
		return ModB(self.val * x)

	def __pow__(self,n): #Supported only if n>=0.
		answer = ModB(1)
		power = self.copy()
		while n > 0:
			if n&1:answer *= power.val
			power *= power.val
			n >>= 1
		return answer
	def __xor__(self,n): #Supported only if B is a prime and val!=0, or n>=0.
		return self ** ( ( n * (2 - ModB.B) )if n < 0 else n )

	def Inverse(n): #Supported only if B is a prime.
		if n < ModB.length_max:
			while len(ModB.inverse) <= n:ModB.inverse+=[ModB.B - ModB.inverse[ModB.B % len(ModB.inverse)] * ( ModB.B // len(ModB.inverse) ) % ModB.B]
			return ModB(ModB.inverse[n],True)
		else:return ModB(n) ** ( ModB.B - 2 )
	def __truediv__(self,x):
		return ModB.Inverse(x) * self
	def __rtruediv__(self,x):
		return x * ModB(ModB.Inverse(self.val),True)

	def Factorial(n):
		while len(ModB.factorial) <= n:ModB.factorial+=[ModB.factorial[-1] * len(ModB.factorial) % ModB.B]
		return ModB(ModB.factorial[n],True)
	def FactorialInverse(n): #Supported only if B is a prime.
		while len(ModB.factorial_inverse) <= n:ModB.factorial_inverse+=[ModB.factorial_inverse[-1] * ModB.Inverse( len(ModB.factorial_inverse) ).val % ModB.B]
		return ModB(ModB.factorial_inverse[n],True)
	def Combination(n,m): #Supported only if B is a prime.
		return ModB.Factorial(n) * (ModB.FactorialInverse(m).val * ModB.FactorialInverse(n-m).val)if 0<=m<=n else ModB(0,True)
ModB.inverse = [None,1 if ModB.B>1 else 0]
ModB.factorial = [1 if ModB.B>1 else 0]
ModB.factorial_inverse = [1 if ModB.B>1 else 0]
def Log(n):
	a=0
	while n>1:n>>=1;a+=1
	return a
s=[
[0,1],
[0,499122176,499122177],
[0,166374059,499122176,332748118],
[0,0,748683265,499122176,748683265],
[0,565671800,0,332748118,499122176,598946612],
[0,0,415935147,0,915057324,499122176,166374059],
[0,308980395,0,831870294,0,499122177,499122176,855638017],
[0,0,582309206,0,956650838,0,83187030,499122176,873463809],
[0,565671800,0,887328314,0,931694729,0,665496236,499122176,443664157],
[0,0,549034394,0,499122177,0,898419917,0,249561089,499122176,299473306],
[0,892369952,0,499122176,0,1,0,998244352,0,831870295,499122176,272248460],
[0,0,915057324,0,374341631,0,831870296,0,374341631,0,415935148,499122176,582309206],
[0,247549973,0,665496237,0,99824432,0,855638020,0,166374057,0,1,499122176,460728163],
[0,0,111708295,0,915057329,0,549034387,0,891289606,0,515759580,0,582309207,499122176,926941185],
[0,166374060,0,188557259,0,166374074,0,99824421,0,277290106,0,565671797,0,166374060,499122176,865145106],
[0,0,249561097,0,457528633,0,415935185,0,811073510,0,415935159,0,457528658,0,748683266,499122176,935854081],
[0,624392049,0,665496282,0,266198402,0,665496322,0,332748070,0,332748135,0,332748113,0,332748119,499122176,939524097],
[0,0,815232828,0,332748316,0,421480688,0,166374243,0,565671719,0,221832103,0,332748112,0,915057325,499122176,720954255],
[0,203902097,0,898419556,0,714,0,655988475,0,332748486,0,199648738,0,34,0,598946605,0,499122178,499122176,105078353],
[0,0,439703392,0,274515479,0,2261,0,434947731,0,831870994,0,149736443,0,855638063,0,274517189,0,83187031,499122176,149736653],
[0,293422811,0,602119123,0,99817563,0,6460,0,301054278,0,604997850,0,998244030,0,808102633,0,499122167,0,665496237,499122176,617960790]
]
coef_prep=[]
coef=[]
D01=21
for k in R(D01):
	coef_prep+=[[ModB(0,True)for k1 in R(k)]]
	for j1 in R(k):
		for j2 in R(j1,k):coef_prep[k][j1]+=ModB.FactorialInverse(k-j2)*ModB.FactorialInverse(j2-j1)*(((k&1)==(j2&1))*2-1)
		coef_prep[k][j1]*=ModB.Factorial(k)*ModB.FactorialInverse(j1)
for j in R(D01):
	coef+=[[]]
	for k in R(D01-j):coef[j]+=[[[coef_prep[k][j1]*s[j][k1]for k1 in R(j+2)]for j1 in R(k)]]
def FloorSumComposition_Body(y,d,q,n,D01):
	global coef_prep
	global coef
	stack=[[y,d,q,n]]
	while True:
		d_0=d%q
		if d_0<1 or n<1:break
		y_0=y%q
		m=(y_0+d_0*(n-1))//q
		y,d,q,n=q+d_0-y_0-1,q,d_0,m
		stack+=[[y,d,q,n]]
	while stack:
		y,d,q,n=stack.pop()
		answer=[]
		assert(q>0 and n >=0 and D01<=len(s))
		n_mod=ModB(n)
		Sn=[ModB(0,True)for j in R(D01)]
		for j in R(D01):
			n_power=ModB(1,True)
			for k in R(1,j+2):
				n_power*=n_mod
				Sn[j]+=s[j][k]*n_power
		d_0=d%q
		temp=[[]for j in R(D01)]
		if d_0<1:
			for j in R(D01):
				temp[j]=[ModB(0,True)for k in R(D01-j)]
				temp[j][0]=Sn[j]
		elif n:
			y_0=y%q
			m=(y_0+d_0*(n-1))//q
			m_mod=ModB(m)
			m_power=[ModB(1,True)]
			for k in R(1,D01):m_power+=[m_power[-1]*m_mod]
			for j in R(D01):
				temp[j]=[ModB(0,True)for k in R(D01-j)]
				for k in R(D01-j):
					coef_jk=coef[j][k]
					temp[j][k]=Sn[j]*m_power[k]
					for j1 in R(k):
						for k1 in R(j+2):temp[j][k]+=prev[j1][k1]*coef_jk[j1][k1]
		d_1=ModB(d//q)
		y_1=ModB(y//q)
		for j in R(D01):
			if(len(answer)<=j):answer+=[[]]
			for k in R(D01-j):
				if len(answer[j])<=k:
					answer[j]+=[ModB(0,True)]
					if n:
						y_1_power=ModB(1,True)
						for k1 in R(k+1):
							d_1_power=ModB(1,True)
							for k2 in R(k-k1+1):
								answer[j][k]+=temp[j+k2][k-k1-k2]*ModB.FactorialInverse(k2)*ModB.FactorialInverse(k1)*ModB.FactorialInverse(k-k1-k2)*y_1_power*d_1_power
								d_1_power*=d_1
							y_1_power*=y_1
						answer[j][k]*=ModB.Factorial(k)
		prev=answer
	return answer
def FloorSumComposition(y,d,q,n,f):
	D0=len(f)
	D01=max(j+len(f[j])for j in R(D0))
	coef=FloorSumComposition_Body(y,d,q,n,D01)
	answer=ModB(0,True)
	for j in R(D0):
		for k in R(len(f[j])):answer+=f[j][k]*coef[j][k]
	return answer
J=lambda:map(int,input().split())
for _ in R(sum(J())):
	p,q,N,M,A,B=J()
	f=[[]for j in R(p+1)]
	f[p]=[ModB(0,True)for k in R(q+1)]
	f[p][q]=ModB(1,True)
	print(FloorSumComposition(B,A,M,N+1,f))
0