HDU2842-Chinese Rings (recursive + matrix fast power), power zero matrix
Question Link
Question: Find the minimum number of steps to solve the problem. The condition for removing the k is that the K-2 has been removed and the k-1 is still on the bracket.
Train of Thought: I think I have played all the nine links. In fact, the minimum step is to start from the last ring and continue to get it out. Therefore, if the steps required to retrieve the first n rings are f (n), then f (n-2) is to be taken out before that, and 1, that is, the n ring is removed, so only the n-1 ring is not removed, then we will set up the previous N-2 ring (set up and take down the steps is the same, f (n-2), so the n-1 ring is taken as f (n-1), so we can get a recursive formula: f (n) = f (n-2) + 1 + f (n-2) + f (n-1) = f (n-1) + 2f (n-2) + 1;
Because n is too large, you can use the Matrix to quickly calculate the power.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;//typedef long long ll;typedef __int64 ll;const int MOD = 200907;struct mat { ll s[3][3]; mat () { memset(s, 0, sizeof(s)); } mat operator * (const mat& c) { mat ans; memset(ans.s, 0, sizeof(ans.s)); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) for (int k = 0; k < 3; k++) ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]) % MOD; return ans; }}tmp, c;ll n;void init() { tmp.s[0][0] = tmp.s[0][2] = tmp.s[1][0] = tmp.s[2][2] = c.s[1][0] = c.s[2][0] = 1; tmp.s[0][1] = c.s[0][0] = 2;}mat pow_mod(ll k) { if (k == 1) return tmp; mat a = pow_mod(k / 2); mat ans = a * a; if (k % 2) ans = ans * tmp; return ans;}int main() { init(); while (scanf("%I64d", &n) && n) { if (n == 1 || n == 2) { printf("%I64d\n", n); continue; } mat ans = pow_mod(n - 2); ans = ans * c; printf("%I64d\n", ans.s[0][0]); } return 0;}
Noip2012 raise group semi-finals question
Day1:
Question 1: suffix array template question, score question.
Question 2: it can be proved that the optimal solution must be a continuous interval. You can enumerate two breakpoints and split the remaining vertices into a charge flow.
Question 3: Consider the exponent of each prime number. When it is not multiplied by its multiples, for example, in the range of p and 2 p, It is a linear recursion, and the matrix quickly loses its power second. Therefore, each prime number is enumerated, and the n/p segment is used as a fast power. However, note that high-precision operations must be optimized using FFT. Some roots can be pre-processed first, or TLE is required.
Day2:
Question 1: optimization of the bare slope to score questions.
Question 2: model conversion is required for the annoying semi-plane intersection.
Question 3: This question is troublesome. It is estimated that there is no perfect algorithm. linear programming is used as the benchmark. In fact, you can press DP when m is relatively small based on the data range. When it is large, various random algorithms can be used.