问候
学了生成函数过后看见什么就想用生成函数搞一搞。
析
背包问题本质上就是生成函数。这里就是要把一堆 给卷起来。然后有一个显然的思路是倍增,用稀疏表预处理一段的价值,然后询问来了暴力拆着卷。不过这个 应该很难过。于是我也很难过,只能想其他办法了!
来了来了。这个题是否可以用前缀和呢?很明显,我们可以求出前缀的生成函数和它们的逆,然后来询问直接右端点(含)原函数卷左端点(不含)逆就可以了。
码
Code.
#include<bits/stdc++.h>
#define int long long
#define y1 qht_yjx
#define hash ZhaoAk
#define maxn 10005
#define endl "\n"
#define nullptr 0
#define mod 1000000007ll
#define rep(i ,a ,b) for (int i = a ;i <= b ;i ++)
#define per(i ,a ,b) for (int i = b ;i >= a ;i --)
#define vt vector
#define pb push_back
#define pii pair <int ,int>
#define I ios :: sync_with_stdio (nullptr);
#define AK cin.tie (nullptr);
#define CSP cout.tie (nullptr);
using namespace std;
int n ,m ,v = 1000;
struct GF
{
int a[maxn][1005];
void clear ()
{
rep (i ,1 ,n)
rep (j ,1 ,v)
a[i][j] = 0;
return ;
}
void jicheng (int pos)
{
memcpy (a[pos] ,a[pos - 1] ,sizeof a[pos - 1]);
return ;
}
void create ()
{
a[0][0] = 1;
return ;
}
void Gopre ()
{
rep (i ,1 ,n)
rep (j ,1 ,v)
a[i][j] = (a[i][j] + a[i][j - 1]) % mod;
return ;
}
void qadd (int pos ,int val)
{
rep (j ,val ,v)
a[pos][j] = (a[pos][j] + a[pos][j - val]) % mod;
return ;
}
void qdlt (int pos ,int val)
{
per (j ,val ,v)
a[pos][j] = (a[pos][j] - a[pos][j - val] + mod) % mod;
return ;
}
} f ,g;
int Convolution (GF &x ,GF &y ,int ps1 ,int ps2 ,int c)
{
int ans = 0;
rep (i ,0 ,c)
ans = (ans + x.a[ps1][i] * y.a[ps2][c - i]) % mod;
return ans;
}
void Gogogo (int idx)
{
cin >> n >> m;
f.clear () ,g.clear () ,f.create () ,g.create ();
rep (i ,1 ,n)
{
int a ,b;
cin >> a >> b;
f.jicheng (i);
g.jicheng (i);
f.qadd (i ,b);
f.qdlt (i ,b * (a + 1));
g.qdlt (i ,b);
g.qadd (i ,b * (a + 1));
}
f.Gopre ();
cout << "Case #" << idx << ": " << endl;
int las = 0;
while (m --)
{
int l ,r ,c;
cin >> l >> r >> c;
l = (l + las) % n + 1 ,r = (r + las) % n + 1;
if (l > r)
swap (l ,r);
las = Convolution (g ,f ,l - 1 ,r ,c);
cout << las << endl;
}
return ;
}
signed main()
{
I AK CSP
int T;
cin >> T;
rep (i ,1 ,T)
Gogogo (i);
return !!!!! ("ShZhao" && "SHzhao");
}
//Code by Lyyq.
//wage.