P4828 Nagisa loves Tomoya

题目传送门

杨辉三角

题外话:大家不会都是搜索 \(\text{Nagisa}\) 进来的吧?(真是 CL 真爱粉了)


我们模拟在若干轮后下标出现的次数,发现就是杨辉三角。

具体地,对于询问 \((x,y)\),答案即为 \(\sum_{i=y}^{x+y} f_{x,i-y} \times a_{i\bmod n}\)。

其中 \(a\) 下标从 \(0\) 开始,\(f\) 为杨辉三角系数。

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
const int M=2000;
const int mod=998244353;
int n;
int a[N],f[M+5][M+5];
void Init(){
    f[0][0]=1;
    for(int i=1;i<=M;i++){
        f[i][0]=1;
        for(int j=1;j<=i;j++) f[i][j]=(f[i-1][j]+f[i-1][j-1])%mod;
    }
}
signed main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++) cin>>a[i];
    Init();
    int Q;
    cin>>Q;
    while(Q--){
        int x,y;
        cin>>x>>y;
        y--;
        int ans=0;
        for(int i=y;i<=y+x;i++) ans=(ans+a[i%n]*f[x][i-y]%mod)%mod;
        cout<<ans<<"\n";
    }
    return 0;
}
posted @ 2025-09-11 17:15  _AzureSky  阅读(19)  评论(0)    收藏  举报