题解:P7618 [COCI 2011/2012 #2] FUNKCIJA
TemplateClass · · 题解
建图,对于两个有关联的循环(假设是第
然后我们开始 dp。设
记
#include<iostream>
#include<vector>
#include<utility>
#include<cctype>
#define x first
#define y second
constexpr int N = 26 + 5;
constexpr int V = 1e5 + 5;
constexpr int MOD = 1000000007;
typedef std::pair<int, int> Range;
int n; Range r[N]; std::vector<int> G[N];
int sum[N][V]; bool calc_ed[N];
inline int gsum(int u, int l, int r) {
return l > r ? 0 : (sum[u][r] - sum[u][l - 1] + MOD) % MOD;
}
int solve_dp(int u, int i) {
int ret = 1;
for(const auto& v : G[u]) {
if(!calc_ed[v] && (calc_ed[v] = true)) {
int pL = r[v].x ? r[v].x : 1, pR = r[v].y ? r[v].y : V - 1;
for(int j = pL; j <= pR; ++j) {
sum[v][j] = (1ull * sum[v][j - 1] + solve_dp(v, j)) % MOD;
}
}
int L = (r[v].x ? r[v].x : i), R = (r[v].y ? r[v].y : i);
ret = (1ull * ret * gsum(v, L, R)) % MOD;
}
return ret;
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0), std::cout.tie(0);
std::cin >> n;
for(int i = 1; i <= n; ++i) {
std::string a, b; std::cin >> a >> b;
bool ia = std::isalpha(a[0]), ib = std::isalpha(b[0]);
r[i].x = ia ? 0 : std::stoi(a), r[i].y = ib ? 0 : std::stoi(b);
int ati = a[0] - 'a' + 1, bti = b[0] - 'a' + 1;
G[ia ? ati : ib ? bti : 0].push_back(i);
}
std::cout << solve_dp(0, 0) << "\n";
return 0;
}