Bestcoder round #8 a, B, c

Source: Internet
Author: User
Bestcoder round #8

Question Link

A: I don't have to mention the question of sign-in.

B: The rapid power of the matrix. The formula of the odd number is F (n) = 4 * F (n-1) + 1. the even number is twice that of the odd number, then, construct a matrix of 4 1 0 1 and perform a quick power.

C: DP + tree array acceleration, DP [I] [J] indicates the number of types whose end length is J with I, and then the number is discretization, each status transition requires the previous interval and transfer, so the tree array can be used for maintenance.

Code:

A:

#include <cstdio>#include <cstring>#include <algorithm>#include <set>using namespace std;typedef long long ll;ll num[105];set<ll> save;int n;int main() {while (~scanf("%d", &n)) {save.clear();for (int i = 0; i < n; i++) {scanf("%I64d", &num[i]);for (int j = 0; j < i; j++) {save.insert(num[i] + num[j]);}}ll ans = 0;for (set<ll>::iterator it = save.begin(); it != save.end(); it++) {ans += *it;}printf("%I64d\n", ans);}return 0;}

B:

#include <cstdio>#include <cstring>#include <cmath>#include <iostream>using namespace std;typedef long long ll;ll n, m;struct mat {ll v[2][2];mat() {memset(v, 0, sizeof(v));}mat operator * (mat c) {mat ans;for (int i = 0; i < 2; i++) {for (int j = 0; j < 2; j++) {for (int k = 0; k < 2; k++) {ans.v[i][j] = (ans.v[i][j] + v[i][k] * c.v[k][j]) % m;}}}return ans;}};mat pow_mod(mat A, ll k) {mat ans;for (int i = 0; i < 2; i++) ans.v[i][i] = 1;while (k) {if (k&1) ans = ans * A;A = A * A;k >>= 1;}return ans;}int main() {while (cin >> n >> m) {mat A;A.v[0][0] = 4; A.v[0][1] = 1;A.v[1][0] = 0; A.v[1][1] = 1;A = pow_mod(A, (n + 1) / 2);ll ans = A.v[0][1];ans %= m;if (n % 2 == 0) ans = ans * 2 % m;cout << ans << endl;}return 0;}

C:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define lowbit(x) (x&(-x))const int N = 10005;const int M = 105;const int MOD = 123456789;int n, m, dp[N][M], bit[105][N];void add(int *bit, int x, int v) {while (x < N) {bit[x] = (bit[x] + v) % MOD;x += lowbit(x);}}int get(int *bit, int x) {int ans = 0;while (x) {ans = (ans + bit[x]) % MOD;x -= lowbit(x);}return ans;}struct Num {int val, rank, id;} num[N];bool cmpid(Num a, Num b) {return a.id < b.id;}bool cmpval(Num a, Num b) {return a.val < b.val;}int main() {while (~scanf("%d%d", &n, &m)) {memset(bit, 0, sizeof(bit));for (int i = 1; i <= n; i++) {scanf("%d", &num[i].val);num[i].id = i;}sort(num + 1, num + n + 1, cmpval);num[1].rank = 2;for (int i = 2; i <= n; i++) {num[i].rank = num[i - 1].rank;if (num[i].val > num[i - 1].val)num[i].rank++;}sort(num + 1, num + n + 1, cmpid);add(bit[0], 1, 1);memset(dp, 0, sizeof(dp));for (int i = 1; i <= n; i++) {for (int j = min(m, i); j >= 1; j--) {int tmp = get(bit[j - 1], num[i].rank - 1);dp[i][j] = (dp[i][j] + tmp) % MOD;add(bit[j], num[i].rank, tmp);}}int ans = 0;for (int i = 1; i <= n; i++)ans = (ans + dp[i][m]) % MOD;printf("%d\n", ans);}return 0;}


Bestcoder round #8 a, B, c

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.