萌新刚学 OI ln1 us 求问 dp
查看原帖
萌新刚学 OI ln1 us 求问 dp
766675
da_ke楼主2024/8/5 23:17
#include <bits/stdc++.h>
#define rep(i,l,r) for(int i=l;i<=r;i++)

using namespace std;

typedef long long ll;
typedef double db;

const int N=114;
const ll mod=998244353;

int n;
ll a[N],mem[N][N][N];

ll dp(int i,int j,int r,int p)
{
    ll& ans=mem[i][j][r];
    if(ans!=-1) return ans;
    if(j==p) return 1;
    else if(j>p) return 0;
    if(i==n+1&&j<p) return 0;
    ll ans0,ans1;
    ans0=dp(i+1,j,r,p)%mod;
    ans1=dp(i+1,j+1,(r+a[i])%p,p)%mod;
    return ans0+ans1;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.in","r",stdin);
    freopen("out.out","w",stdout);
#endif
    cin>>n;
    rep(i,1,n) cin>>a[i];
    ll ans=0;
    rep(i,1,n)
    {
        rep(i1,1,n) rep(i2,1,n) rep(i3,1,n) mem[i1][i2][i3]=-1;
        ans=ans+dp(1,0,0,i);ans%=mod;
    }
    cout<<ans;
}

样例一没过,样例二过了。是记搜,应该算是 dp,很另类,但转移应该对了(?)

2024/8/5 23:17
加载中...