Topic Portal
A. Taobao commodity prices big PK
sol: Enumeration delete each number, and then seek the maximum rise sequences.
- enumerate
#include "bits/stdc++.h" using namespace std; const int MAXN = 105; int arr[MAXN], n, ans; map<int, int>::iterator it; int getLIS(int k) { map<int, int> mp; int ans = 0; for (int i = 1; i <= n; i++) { if (i == k) continue; int mx = 0; for (it = mp.begin(); it != mp.end() && it->first < arr[i]; it++) if (it->second > mx) mx = it->second; mp[arr[i]] = mx + 1; ans = max(ans, mp[arr[i]]); } return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); int k = getLIS(0); for (int i = 1; i <= n; i++) if (getLIS(i) < k) ans ++; printf("%d\n", ans); return 0; }
B. Alibaba help battle SARS (simple)
sol:
dp [i] [1] and the number i represents the length of 'A' is an odd number 'C' is the number of an even number; dp [i] [1] = dp [i - 1] [1] * 2 + dp [i - 1] [3] + dp [i - 1] [4];
dp [i] [2] where i represents the length and the number of 'A' is an even number 'C' is an odd number; dp [i] [2] = dp [i - 1] [2] * 2 + dp [i - 1] [3] + dp [i - 1] [4];
dp [i] [3] where i represents the length and the number of 'A' is an odd number of 'C' is an odd number; dp [i] [3] = dp [i - 1] [3] * 2 + dp [i - 1] [1] + dp [i - 1] [2];
dp [i] [4] and the number i represents the length of 'A' is an even number 'C' is an even number; dp [i] [4] = dp [i - 1] [4] * 2 + dp [i - 1] [1] + dp [i - 1] [2];
- Dynamic Programming
#include "bits/stdc++.h" using namespace std; const int MOD = 1e9 + 7; const int MAXN = 1005; int dp[MAXN][5]; void init() { dp[0][4] = 1; for (int i = 1; i <= 1000; i++) { dp[i][1] = (dp[i - 1][1] * 2LL + dp[i - 1][3] + dp[i - 1][4]) % MOD; dp[i][2] = (dp[i - 1][2] * 2LL + dp[i - 1][3] + dp[i - 1][4]) % MOD; dp[i][3] = (dp[i - 1][3] * 2LL + dp[i - 1][1] + dp[i - 1][2]) % MOD; dp[i][4] = (dp[i - 1][4] * 2LL + dp[i - 1][1] + dp[i - 1][2]) % MOD; } } int main() { init(); int n; while (scanf("%d", &n) && n) { printf("%d\n", dp[n][4]); } }
C. Alibaba help battle SARS (medium)
sol1: observation of a problem on a state transition matrix can be transformed into equation found quickly solved Power
- Fast power matrix
#include "bits/stdc++.h" using namespace std; const int MOD = 1e9 + 7; struct Mat { int mat[5][5]; Mat() {memset(mat, 0, sizeof(mat));} friend Mat operator * (Mat a, Mat b) { Mat c; for (int k = 1; k <= 4; k++) for (int i = 1; i <= 4; i++) for (int j = 1; j <= 4; j++) c.mat[i][j] = (c.mat[i][j] + 1LL * a.mat[i][k] * b.mat[k][j]) % MOD; return c; } }; Mat mat_pow(Mat n, int k) { Mat ans; for (int i = 1; i <= 4; i++) ans.mat[i][i] = 1; while (k) { if (k & 1) ans = ans * n; n = n * n; k >>= 1; } return ans; } int main() { int k; while (scanf("%d", &k) && k) { Mat m; m.mat[1][1] = m.mat[2][2] = m.mat[3][3] = m.mat[4][4] = 2; m.mat[1][3] = m.mat[1][4] = m.mat[2][3] = m.mat[2][4] = 1; m.mat[3][1] = m.mat[4][1] = m.mat[3][2] = m.mat[4][2] = 1; m = mat_pow(m, k); printf("%d\n", m.mat[4][4]); } return 0; }
sol2: official solution to a problem is a common formula to solve the rapid power, but require a series of mathematical transformations. I do not understand. Concluded that DP [n-] [. 4] = (2 n--. 1 ) * (2 n-. 1- + 1'd)
- Fast power
#include "bits/stdc++.h" using namespace std; const int MOD = 1e9 + 7; int quick_pow(int n, int k) { int ans = 1; while (k) { if (k & 1) ans = 1LL * ans * n % MOD; n = 1LL * n * n % MOD; k >>= 1; } return ans; } int main() { int k; while (scanf("%d", &k) && k) { int p = quick_pow(2, k - 1); printf("%d\n", 1LL * p * (p + 1) % MOD); } return 0; }
D. Alibaba help battle SARS (difficult)
sol1: see a kind of said solution is a decimal power of fast, I thought for a moment in front of the name to know how to write in the group, but this rapid power matrix used in overtime. It should be a constant matrix of relatively large bar.
- Decimal fast inverse power +
#include "bits/stdc++.h" using namespace std; const int MOD = 1e9 + 7; const int MAXN = 1e5 + 5; char k[MAXN]; int quick_pow_2(int n, int k) { int ans = 1; while (k) { if (k & 1) ans = 1LL * ans * n % MOD; n = 1LL * n * n % MOD; k >>= 1; } return ans; } int quick_pow_10(int n, char* k) { int ans = 1; int l = strlen(k); for (int i = l - 1; i >= 0; i--) { ans = 1LL * ans * quick_pow_2(n, k[i] ^ '0') % MOD; n = quick_pow_2(n, 10); } return ans; } int main() { while (scanf(" % S " , K)) { IF (strlen (K) == . 1 && K [ 0 ] == ' 0 ' ) BREAK ; int P = quick_pow_10 ( 2 , K); // because precision subtraction trouble, and As already written power of fast, so we used the inverse of multiplication to solve the more than 2 go; the p-* = 1LL the p-* quick_pow_2 ( 2 , MOD - 2 )% MOD; printf ( " % d \ the n- " , 1LL * * P (P + . 1 )% the MOD); } return 0 ; }
sol2: you can use Fermat's Little Theorem to solve the problem with high precision. Write inverse time been using Fermat's little theorem, but can not think of a place outside. This solution matrix fast power will not be timed out.
- Fermat's Little Theorem
#include "bits/stdc++.h" using namespace std; const int MOD = 1e9 + 7; const int MAXN = 1e5 + 5; struct Mat { int mat[5][5]; Mat() {memset(mat, 0, sizeof(mat));} friend Mat operator * (Mat a, Mat b) { Mat c; for (int k = 1; k <= 4; k++) for (int i = 1; i <= 4; i++) for (int j = 1; j <= 4; j++) c.mat[i][j] = (c.mat[i][j] + 1LL * a.mat[i][k] * b.mat[k][j]) % MOD; return c; } }; Mat mat_pow(Mat n, int k) { Mat ans; for (int i = 1; i <= 4; i++) ans.mat[i][i] = 1; while (k) { if (k & 1) ans = ans * n; n = n * n; k >>= 1; } return ans; } char s[MAXN]; int main() { while (scanf("%s", &s)) { if (strlen(s) == 1 && s[0] == '0') break; Mat m; int k = 0; for (int i = 0; s[i]; i++) k = (k * 10LL + s[i] - '0') % (MOD - 1); m.mat[1][1] = m.mat[2][2] = m.mat[3][3] = m.mat[4][4] = 2; m.mat[1][3] = m.mat[1][4] = m.mat[2][3] = m.mat[2][4] = 1; m.mat[3][1] = m.mat[4][1] = m.mat[3][2] = m.mat[4][2] = 1; m = mat_pow(m, k); printf("%d\n", m.mat[4][4]); } return 0; }