Effect: Given $ x, q $, $ X $ each step becomes equal probability $ [x, q] $ any one number, find becomes a desired operand of $ Q $.
It is easy to get $ f (x, q) = \ frac {\ sum \ limits_ {i = x + 1} ^ qf (i, q) + qx + 1} {qx} $, $ F boundary conditions ( q, q) = 0 $.
Each query complexity is $ O (q) $, but the answer can be found only on the difference between the $ X $ and $ Q $, so the pre-treatment results to $ q = at N-1 $.
#include <iostream> #include <cstdio> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) using namespace std; typedef long long ll; const int P = 998244353; const int N = 1e7+10; int dp[N], inv[N]; int main() { inv[0]=inv[1]=1; REP(i,2,N-1) inv[i]=(ll)inv[P%i]*(P-P/i)%P; int sum = 0; PER(i,0,N-2) { dp[i] = (ll)(sum+N-i)*inv[N-1-i]%P; (sum += dp[i]) %= P; } int t; scanf("%d", &t); while (t--) { int x, q; scanf("%d%d", &x, &q); printf("%d\n",dp[N-1-q+x]); } }