QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#232827 | #6410. Classical DP Problem | Laurra | TL | 0ms | 0kb | C++14 | 1.4kb | 2023-10-30 23:16:04 | 2023-10-30 23:16:05 |
answer
#include <iostream>
#include <vector>
using namespace std;
#define MOD 998244353
#define dim 5002
#define ll long long
vector<int> v;
int n;
bool ok(int i,int j)
{
if(i>=1 && v[i]>=j)
return true;
}
int dp[dim][dim];
ll fact(int k)
{
ll p=1,i;
for(i=2;i<=k;i++)
p*=i;
return p;
}
void init()
{
int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
dp[i][j]=0;
}
}
ll calc(int k)
{
int x=v[k+1],i,j;
dp[0][x]=1;
for(i=0;i<n;i++)
{
for(j=0;j<=x;j++)
{
if(j)
{
dp[i+1][j-1]=(dp[i+1][j-1]+j*dp[i][j])%MOD;
}
dp[i+1][j]=(dp[i+1][j]+(v[i+1]-j)*dp[i][j])%MOD;
}
}
return dp[k][0];
}
void flip()
{
vector<int> v2(n+1);
int i,j;
for(i=1;i<=n;i++)
{
j=1;
while(j<=n && v[j]>=i)
j++;
v2[i]=j-1;
}
v=v2;
}
int main()
{
int k,j,i;
ll tot=0;
//Citire
cin>>n;
v.resize(n+1);
for(i=1;i<=n;i++)
{
cin>>v[n-i+1];
}
//Calc nr ture
k=0;
j=1;
i=1;
while(ok(i,j)==true)
{
i++;
j++;
k++;
}
cout<<k<<" ";
tot=-fact(k);
//Dinamica
init();
tot+=calc(k);
flip();
init();
tot+=calc(k);
cout<<tot;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 1 2 3