Brine
A certain person trying to make his way in the universe.
22527 鄭竣陽
Brine
BrineTW#7355
Index
Divide & Conquer
typedef vector<int>::iterator It;
void mergesort(It l, It r) {
if (r - l == 1) return;
It m = l + (r - l >> 1);
mergesort(l, m), mergesort(m, r);
vector<int> a(l, m), b(m, r);
a.push_back(INT32_MAX), b.push_back(INT32_MAX);
It afront = a.begin(), bfront = b.begin();
for (It it = l; it < r; ++it) {
*it = (*afront <= *bfront ? *afront++ : *bfront++);
}
}
typedef long long ll;
const int M = 1e9 + 225;
int power(ll a, int n) {
if (n == 1) return a;
ll half = power(a, n / 2);
ll full = half * half % M;
if (n % 2 == 0) return full;
return full * a % M;
}
typedef long long ll;
const int M = 1e9 + 225;
int powerBySquaring(int a, int n) {
ll current = a;
ll answer = 1;
while (n > 0) {
if (n % 2 == 1) answer = answer * current % M;
current = current * current % M;
n /= 2;
}
}
Dynamic Programming
int f(int n) {
if (n < 2) return n;
return f(n - 1) + f(n - 2);
}
vector<int> dp(30, -1);
int f(int n) {
if (dp[n] >= 0) return dp[n];
return dp[n] = f(n - 1) + f(n - 2);
}
int main() {
dp[0] = 0;
dp[1] = 1;
int q;
while (cin >> q) cout << f(q) << '\n';
}
vector<int> dp(30);
int main() {
dp[1] = 1;
for (int i = 2; i < dp.size(); i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
int q;
while (cin >> q) cout << f[q] << '\n';
}
DP Classics
Maximum Consecutive Sum
Paths on grids
起點 | ||||||||
---|---|---|---|---|---|---|---|---|
卡 | 卡 | |||||||
卡 | ||||||||
終點 |
Longest Common Subsequence
By Brine
演算法[7]