\(\mathcal{Description}\)
Link.
有 \(n\) 种物品,第 \(i\) 中有 \(a_i\) 个,单价为 \(b_i\)。共 \(q\) 次询问,每次查询用不超过 \(c\) 的钱购买种类在 \([l,r]\) 之中的物品,有多少种方案。强制在线;答案对 \(998244353\) 取模。
\(n\le10^4\),\(q\le5\times10^4\),\(c\le10^3\)。
\(\mathcal{Solution}\)
快速回答区间询问,最基础但容易被忽略的处理方式——前缀和差。
考虑第 \(i\) 中物品的 OGF,显然有
\[G_i(x)=\frac{1-x^{(a_i+1)b_i}}{1-x^{b_i}}. \]欲求答案 \(\sum_{k\le c}[x^k]\prod_{i=l}^rG_i(x)\),转化为前缀积乘上前缀积的逆,预处理出
\[S_i(x)=\prod_{j=1}^iG_j(x),\\ S^{-1}_i(x)=\prod_{j=1}^iG_j^{-1}(x). \]顺带发现 \(G_j(x)\) 和 \(G_j^{-1}\) 长相完全一样,所以这俩也就是换换加减号的事儿。精巧递推一发可以做到 \(\mathcal O(nc)\) 预处理,查询复杂度即求前缀系数和,预先将 \(S_i(x)\) 或 \(S_i^{-1}(x)\) 的系数做前缀和后即为求卷积的某项系数,暴力模拟,则有单次查询复杂度 \(\mathcal O(c)\)。
\(\mathcal{Code}\)
/* Clearink */
#include <cstdio>
#include <cstring>
#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )
inline int rint() {
int x = 0, s = getchar();
for ( ; s < '0' || '9' < s; s = getchar() );
for ( ; '0' <= s && s <= '9'; s = getchar() ) x = x * 10 + ( s ^ '0' );
return x;
}
inline void wint( const int x ) {
if ( 9 < x ) wint( x / 10 );
putchar( x % 10 ^ '0' );
}
const int MAXN = 1e4, MAXC = 1e3, MOD = 998244353;
int n, q, a[MAXN + 5], b[MAXN + 5];
int f[MAXN + 5][MAXC + 5], g[MAXN + 5][MAXC + 5];
inline void subeq( int& a, const int b ) { ( a -= b ) < 0 && ( a += MOD ); }
inline int sub( int a, const int b ) { return ( a -= b ) < 0 ? a + MOD : a; }
inline void addeq( int& a, const int b ) { ( a += b ) >= MOD && ( a -= MOD ); }
inline int add( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
inline int mul( const long long a, const int b ) { return int( a * b % MOD ); }
inline void init() {
f[0][0] = g[0][0] = 1;
rep ( i, 1, n ) {
memcpy( f[i], f[i - 1], sizeof f[i] );
memcpy( g[i], g[i - 1], sizeof g[i] );
int t;
rep ( j, t = b[i], MAXC ) addeq( f[i][j], f[i][j - t] );
per ( j, MAXC, t = ( a[i] + 1 ) * b[i] ) subeq( f[i][j], f[i][j - t] );
rep ( j, t = ( a[i] + 1 ) * b[i], MAXC ) addeq( g[i][j], g[i][j - t] );
per ( j, MAXC, t = b[i] ) subeq( g[i][j], g[i][j - t] );
}
rep ( i, 0, n ) rep ( j, 1, MAXC ) addeq( g[i][j], g[i][j - 1] );
}
int main() {
freopen( "shop.in", "r", stdin );
freopen( "shop.out", "w", stdout );
n = rint(), q = rint();
rep ( i, 1, n ) a[i] = rint(), b[i] = rint();
init();
for ( int ans = 0, l, r, c; q--; ) {
l = ( rint() + ans ) % n + 1, r = ( rint() + ans ) % n + 1, c = rint();
if ( l > r ) l ^= r ^= l ^= r;
ans = 0;
rep ( i, 0, c ) addeq( ans, mul( f[r][i], g[l - 1][c - i] ) );
wint( ans ), putchar( '\n' );
}
return 0;
}