公式的话官方题解已经非常详细,这里就不再写公式了,大致推导为n步有x+y步是j,k两维移动,有n-x-y步是在i轴上移动。 在x+y的两维中,有y步是在y轴上移动,x步在x轴上移动。然后算上C(n,x+y)*C(x+y,x)*t1^(x/p)*t2(y/p)。就是每个点的贡献。这题卡常卡的太恶心了。
#include"bits/stdc++.h"
using namespace std;
typedef long long LL;
typedef long long ll;
const LL mod = 998244353;
LL pi = 2;
LL pe = 8388608, Pa[8388610];
LL per = pe-1;
LL fac[2][30],inv[2][30],mul0,mul1,mul2,qpe[1007];
inline LL qpow(LL a, LL b, LL m)
{
LL ans = 1;
a %= m;
while(b > 0)
{
if(b&1) ans = ans*a%m;
a = a*a%m;
b >>= 1;
}
return ans;
}
inline LL fast_pow(LL a, LL b, LL m)
{
LL ans = 1;
LL mm = m-1;
a &= mm;
while(b > 0)
{
if(b&1) ans = ans*a&mm;
a = a*a&mm;
b >>= 1;
}
return ans;
}
inline LL exgcd(LL a,LL b,LL &x,LL &y) {
if(b==0) {
x=1,y=0;
return a;
}
LL d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
inline LL Inv(LL a,LL P){//求a在膜P下的逆
if(a==0)return 1;
LL x,y;
exgcd(a,P,x,y);
return (x%P+P)%P;
}
inline LL cal1(LL n) {
LL ans=0;
while(n > 0) {
ans += (n>>1);
n >>= 1;
}
return ans;
}
inline LL cal2(LL n) {
if(n == 0) return 1;
LL ans = 1;
if(n/pe) ans = qpe[n/pe];
ans = Pa[n&per];
return cal2(n>>1)*ans&per;
}
inline LL cal(LL n,LL m, LL P) {
LL cnt = cal1(n) - cal1(m) - cal1(n-m);
LL a = cal2(n), b = cal2(m), c = cal2(n-m);
LL ans = ((a*Inv(b,pe)&per)*Inv(c,pe)&per)*fast_pow(pi,cnt,pe)&per;
return ans;//范围可到达P*Pi,注意是否用快速乘
}
inline LL C(LL n, LL m, LL p, LL id)//组合数C(n, m) % p
{
if(m > n) return 0;
return fac[id][n] * inv[id][m]%p * inv[id][n - m] % p;
}
inline LL Lucas(LL n, LL m, LL p, LL id)
{
if(m == 0) return 1;
return C(n % p, m % p, p, id) * Lucas(n / p, m / p, p, id) % p;
}
inline LL solve(LL n,LL m,LL P) {
if(m>n)return 0;
LL ans = cal(n,m,P)*mul0%P;
LL p = 7;
LL t1 = Lucas(n,m,p,0)*mul1%P;
p = 17;
LL t2 = Lucas(n,m,p,1)*mul2%P;
ans = (ans + t1 + t2)%P;
return ans;
}
void init(LL P){
mul0 = (P/pe)*Inv(P/pe,pe)%P;
mul1 = (P/7)*Inv(P/7,7)%P;
mul2 = (P/17)*Inv(P/17,17)%P;
Pa[0] = Pa[1] = 1;
for(LL j = 2; j <= pe; j++){
if(j&1) Pa[j] = Pa[j-1]*j&per;
else Pa[j] = Pa[j-1];
}
qpe[0] = Pa[pe];
for(int i = 1; i <= 1000; i++)
qpe[i] = qpe[i-1]*Pa[pe]%pe;
LL p = 7;
fac[0][0] = fac[0][1] = 1;
inv[0][0] = inv[0][1] = 1;
for(LL j = 2; j <= p; j++){
fac[0][j] = fac[0][j-1]*j%p;
inv[0][j] = inv[0][j-1]*qpow(j,p-2,p)%p;
}
p = 17;
fac[1][0] = fac[1][1] = 1;
inv[1][0] = inv[1][1] = 1;
for(LL j = 2; j <= p; j++){
fac[1][j] = fac[1][j-1]*j%p;
inv[1][j] = inv[1][j-1]*qpow(j,p-2,p)%p;
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t1,t2,p,q,n,m;
init(mod-1);
while(~scanf("%d%d%d%d%d%d",&t1,&t2,&p,&q,&n,&m)){
LL ans = 1;
for(LL i = 1; i <= m; i++){
int ax,ay,av;
scanf("%d%d%d",&ax,&ay,&av);
if(ax%p != 0 || ay%q != 0){
continue;
}
LL x = ax/p , y = ay/q;
if(x+y > n) continue;
LL tmp = solve(x+y, x, mod-1)*solve(n,x+y,mod-1)%(mod-1)*qpow(t1,x,mod-1)%(mod-1)*qpow(t2,y,mod-1)%(mod-1);
ans = ans*qpow(av,tmp,mod)%mod;
}
printf("%lld\n",(ans%mod+mod)%mod);
}
return 0;
}