problem1 linknode
對於數字$x$,檢驗每一個知足$x=y*2^{t}$的$y$可否變成$x$便可。code
problem2 linkblog
若是起點到終點有一條長度爲$L$的路徑,那麼就存在長度爲$L+kR$的路徑。其中$R$爲從路徑上某點轉一圈再回到這一點的環的長度。ip
爲了保證老是存在這個環,能夠令這個環爲從起點出發再回到起點。因此若是有一條長度爲$d$的邊$0\rightarrow t$,那麼能夠令$R=2d$,即$0\rightarrow t \rightarrow 0$.get
只須要記錄起點到達某個點長度模$R$的最短路便可。即用$f[m][u]$表示從0到$u$的最小的知足$m+kR$的路徑長度。string
只要$f[T$%$R][N-1] \leq T$便可。it
problem3 linkast
紅色和綠色須要配對出現。因此能夠將一個紅色一個綠色看做一個總體。那麼就是$M$組中每組要出現$D$個配對的總體。class
從前向後進行動態規劃,只統計出現了多少個配對的總體以及還有多少個只配對了一半的。統計
這裏有個問題是每一組中的$D$個總體不能交叉出現。爲了作到這一點,只須要配對了一半的個數不超過$M$便可.
code for problem1
#include <set> #include <vector> class AmebaDiv1 { public: int count(const std::vector<int> &X) { auto Get = [&](int t) { for (auto e : X) { if (e == t) { t *= 2; } } return t; }; std::set<int> all(X.begin(), X.end()); int result = 0; for (auto e : all) { bool tag = (Get(1) != e); int t = e; while (t > 1) { if (Get(t) == e) { tag = false; break; } t /= 2; } if (tag) { ++result; } } return result; } };
code for problem2
#include <cstring> #include <set> #include <string> #include <vector> constexpr int MAXD = 20000; constexpr int MAXN = 50; long long dist[MAXD][MAXN]; class LongLongTripDiv1 { public: std::string isAble(int N, const std::vector<int> &A, const std::vector<int> &B, const std::vector<int> &D, long long T) { int n = static_cast<int>(A.size()); int d = MAXD + 1; for (int i = 0; i < n; ++i) { if (A[i] == 0 || B[i] == 0) { d = std::min(d, D[i]); } } if (d == MAXD + 1) { return "Impossible"; } std::set<std::pair<long long, std::pair<int, int>>> que; memset(dist, -1, sizeof(dist)); auto Insert = [&](long long dis, int u, int v) { auto iter = que.find({dist[u][v], {u, v}}); if (iter != que.end()) { que.erase(iter); } que.insert({dis, {u, v}}); dist[u][v] = dis; }; Insert(0, 0, 0); while (!que.empty()) { auto node = que.begin()->second; que.erase(que.begin()); int u = node.second; for (int i = 0; i < n; ++i) { if (A[i] != u && B[i] != u) { continue; } int v = A[i] + B[i] - u; int t = (node.first + D[i]) % (2 * d); long long new_dist = dist[node.first][u] + D[i]; if (dist[t][v] == -1 || new_dist < dist[t][v]) { Insert(new_dist, t, v); } } } auto min_dist = dist[T % (d * 2)][N - 1]; return (min_dist != -1 && min_dist <= T) ? "Possible" : "Impossible"; } };
code for problem3
#include <string> constexpr int MOD = 1000000007; int f[5005][50][51]; class AlternativePiles { public: int count(const std::string &C, int M) { if (Red(C[0])) { f[0][0][1] += 1; } if (Blud(C[0])) { f[0][0][0] += 1; } for (size_t idx = 1; idx < C.size(); ++idx) { char c = C[idx]; for (int x = 0; x < M; ++x) { for (int y = 0; y <= M; ++y) { int t = f[idx - 1][x][y]; if (t == 0) { continue; } if (Red(c) && y + 1 <= M) { (f[idx][x][y + 1] += t) %= MOD; } if (Blud(c)) { (f[idx][x][y] += t) %= MOD; } if (Green(c) && y > 0) { (f[idx][(x + 1) % M][y - 1] += t) %= MOD; } } } } return f[C.size() - 1][0][0]; } private: bool Red(char c) { return c == 'R' || c == 'W'; } bool Green(char c) { return c == 'G' || c == 'W'; } bool Blud(char c) { return c == 'B' || c == 'W'; } };