Submission #6188060
Source Code Expand
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
using namespace std;
template <typename X, typename T> auto make_vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto make_vectors(X x, Y y, Z z, Zs... zs) { auto cont = make_vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }
template <int32_t MOD>
struct mint {
int32_t value;
mint() : value() {}
mint(int64_t value_) : value(value_ < 0 ? value_ % MOD + MOD : value_ >= MOD ? value_ % MOD : value_) {}
inline mint<MOD> operator + (mint<MOD> other) const { int32_t c = this->value + other.value; return mint<MOD>(c >= MOD ? c - MOD : c); }
inline mint<MOD> operator - (mint<MOD> other) const { int32_t c = this->value - other.value; return mint<MOD>(c < 0 ? c + MOD : c); }
inline mint<MOD> operator * (mint<MOD> other) const { int32_t c = (int64_t)this->value * other.value % MOD; return mint<MOD>(c < 0 ? c + MOD : c); }
inline mint<MOD> & operator += (mint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
inline mint<MOD> & operator -= (mint<MOD> other) { this->value -= other.value; if (this->value < 0) this->value += MOD; return *this; }
inline mint<MOD> & operator *= (mint<MOD> other) { this->value = (int64_t)this->value * other.value % MOD; if (this->value < 0) this->value += MOD; return *this; }
};
template <int32_t MOD> mint<MOD> operator * (int64_t value, mint<MOD> n) { return mint<MOD>(value) * n; }
template <int32_t MOD> std::ostream & operator << (std::ostream & out, mint<MOD> n) { return out << n.value; }
constexpr int MOD = 1e9 + 7;
mint<MOD> solve(int n, int m, const vector<int> & a, const vector<int> & b) {
auto dp = make_vectors(n + 1, m + 1, mint<MOD>());
auto sum = make_vectors(n + 1, m + 1, mint<MOD>());
dp[0][0] = 1;
REP (i, n + 1) {
REP (j, m + 1) {
if (i - 1 >= 0) sum[i][j] += sum[i - 1][j];
if (j - 1 >= 0) sum[i][j] += sum[i][j - 1];
if (i - 1 >= 0 and j - 1 >= 0) sum[i][j] -= sum[i - 1][j - 1];
if (i - 1 >= 0 and j - 1 >= 0) sum[i][j] += dp[i - 1][j - 1];
if (i - 1 >= 0 and j - 1 >= 0 and a[i - 1] == b[j - 1]) {
dp[i][j] = sum[i][j];
}
}
}
mint<MOD> ans = 0;
REP (i, n + 1) {
REP (j, m + 1) {
ans += dp[i][j];
}
}
return ans;
}
int main() {
int n, m; cin >> n >> m;
vector<int> a(n);
REP (i, n) {
cin >> a[i];
}
vector<int> b(m);
REP (j, m) {
cin >> b[j];
}
cout << solve(n, m, a, b) << endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
E - Common Subsequence |
User |
kimiyuki |
Language |
C++14 (GCC 5.4.1) |
Score |
500 |
Code Size |
2726 Byte |
Status |
AC |
Exec Time |
79 ms |
Memory |
31744 KiB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
500 / 500 |
Status |
|
|
Set Name |
Test Cases |
Sample |
s1.txt, s2.txt, s3.txt, s4.txt, s5.txt |
All |
01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, s1.txt, s2.txt, s3.txt, s4.txt, s5.txt |
Case Name |
Status |
Exec Time |
Memory |
01.txt |
AC |
1 ms |
256 KiB |
02.txt |
AC |
1 ms |
256 KiB |
03.txt |
AC |
1 ms |
256 KiB |
04.txt |
AC |
1 ms |
256 KiB |
05.txt |
AC |
1 ms |
256 KiB |
06.txt |
AC |
67 ms |
30208 KiB |
07.txt |
AC |
66 ms |
31232 KiB |
08.txt |
AC |
59 ms |
29440 KiB |
09.txt |
AC |
59 ms |
29824 KiB |
10.txt |
AC |
55 ms |
29184 KiB |
11.txt |
AC |
57 ms |
30592 KiB |
12.txt |
AC |
55 ms |
30336 KiB |
13.txt |
AC |
55 ms |
30720 KiB |
14.txt |
AC |
54 ms |
30976 KiB |
15.txt |
AC |
51 ms |
29952 KiB |
16.txt |
AC |
78 ms |
28800 KiB |
17.txt |
AC |
79 ms |
31360 KiB |
18.txt |
AC |
71 ms |
29184 KiB |
19.txt |
AC |
71 ms |
29568 KiB |
20.txt |
AC |
71 ms |
29568 KiB |
21.txt |
AC |
73 ms |
30720 KiB |
22.txt |
AC |
72 ms |
29952 KiB |
23.txt |
AC |
3 ms |
1280 KiB |
24.txt |
AC |
41 ms |
27904 KiB |
25.txt |
AC |
4 ms |
1792 KiB |
26.txt |
AC |
4 ms |
1664 KiB |
27.txt |
AC |
54 ms |
31744 KiB |
28.txt |
AC |
54 ms |
31744 KiB |
s1.txt |
AC |
1 ms |
256 KiB |
s2.txt |
AC |
1 ms |
256 KiB |
s3.txt |
AC |
1 ms |
256 KiB |
s4.txt |
AC |
1 ms |
256 KiB |
s5.txt |
AC |
1 ms |
256 KiB |