Submission #126060

# Submission time Handle Problem Language Result Execution time Memory
126060 2019-07-06T22:01:19 Z Mahmoud_Adel Calvinball championship (CEOI15_teams) C++14
100 / 100
279 ms 888 KB
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const int N = 1e4+5, mod = 1e6+7;
ll n, dp[2][N][2], a[N];
ll sol(int i, ll j, int bit)
{
	/*
	if(i == n) return 1;
	ll &ret = dp[i][j][bit];
	if(ret != -1) return ret;
	if(bit) ret = j*sol(i+1, j, 1)%mod + sol(i+1, j+1, 1)%mod;
	else ret = (a[i]-1)*sol(i+1, max(j, a[i]-1), 1)%mod + 
	sol(i+1, max(j, a[i]), 0)%mod;
	return ret%mod;
	*/
	for(int i=0; i<=n; i++) dp[n%2][i][0] = dp[n%2][i][1] = 1LL;
	for(int i=n-1; i>=0; i--)
	{
		for(ll j=0; j<=i; j++)
		{
			dp[i%2][j][1] = j*dp[(i+1)%2][j][1] + dp[(i+1)%2][j+1][1], dp[i%2][j][1] %= mod;
			dp[i%2][j][0] = (a[i]-1)*dp[(i+1)%2][max(j, a[i]-1)][1] +
			dp[(i+1)%2][max(j, a[i])][0];
			dp[i%2][j][0] %= mod;
		}
	}
	return dp[0][0][0]%mod;
}
int main()
{
	memset(dp, -1, sizeof dp);
	cin >> n;
	for(int i=0; i<n; i++) cin >> a[i];
	cout << sol(0, 0, 0) << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 604 KB Output is correct
2 Correct 3 ms 632 KB Output is correct
3 Correct 3 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 636 KB Output is correct
2 Correct 5 ms 636 KB Output is correct
3 Correct 5 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 267 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 71 ms 632 KB Output is correct
2 Correct 71 ms 736 KB Output is correct
3 Correct 74 ms 772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 276 ms 748 KB Output is correct
2 Correct 279 ms 888 KB Output is correct
3 Correct 276 ms 792 KB Output is correct