Subject:
https://ac.nowcoder.com/acm/problem/15251
It is known that f (1, 1) = 1 f(1,1)=1f(1,1)=1,有
f ( i , j ) = a ∗ f ( i − 1 , j ) + b ∗ f ( i − 1 , j − 1 ) i ≥ 2 , 1 ≤ j ≤ i f(i,j)=a*f(i-1,j)+b*f(i-1,j-1)\quad i\ge 2,1\le j\le i f(i,j)=a∗f(i−1,j)+b∗f(i−1,j−1)i≥2,1≤j≤i For other casesf (i, j) = 0 f(i,j)=0f(i,j)=0
hasTTGroup T asks, each timea, b, n, ma,b,n,m are givena,b,n,m,求 f ( n , m ) m o d ( 998244353 ) f(n,m) mod (998244353) f(n,m)mod(998244353)
Idea:
Similar to Problem F: Frightful Formula , except that it turns down to go down to the right, the initial position is (1, 1) (1,1)(1,1 ) , then the number of options is(n − 1 m − 1) {n-1\choose m-1}(m−1n−1) . Sof (1, 1) f(1,1)f(1,The contribution of 1 ) isbm − 1 an − m (n − 1 m − 1) b^(m-1)a^(nm)(n-1\choose m-1)bm−1an−m(m−1n−1)。
#include<bits/stdc++.h>
#define ll long long
#define mod 998244353
using namespace std;
const int N=100009;
int T;
ll p[N],p1[N];
ll qpow(ll a,ll b) {
ll res=1;
a%=mod;
while(b) {
if(b&1)
res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
int main() {
cin>>T;
p[0]=1;
for(int i=1; i<N; i++)
p[i]=p[i-1]*i%mod;
p1[N-1]=qpow(p[N-1],mod-2);
for(int i=N-2; i>=0; i--)
p1[i]=p1[i+1]*(i+1)%mod;
while(T--) {
ll a,b,n,m;
cin>>a>>b>>n>>m;
if(n>=1&&m>=1&&m<=n)
cout<<qpow(b,m-1)*qpow(a,n-m)%mod*p[n-1]%mod*p1[m-1]%mod*p1[n-m]%mod<<endl;
else
cout<<0<<endl;
}
return 0;
}