程序设计思维与实践 Week14 作业

D - Q老师染砖

Description

Input

Output

Sample Input
2
1
2
Sample Output
2
6

• 首先显然是个DP，第i个砖可以有i-1个砖推过来，A[i]代表到i块转红绿均为偶，B[i]奇奇，C[i]奇偶
• A[i]=2 * A[i-1]+C[i-1]
• B[i]=2 * B[i-1]+C [i-1]
• C[i]=2 * A[i-1]+2 * B[i-1]+2 * C[i-1]
• n太大了，普通做法是不行的
• 可以看到当前状态可以有上一个形式一样的状态得到，那么可以用矩阵

#include <iostream>
using namespace std;

const int N = 3;
struct Matrix
{
int x[N][N];
Matrix() { memset(x, 0, sizeof(x)); }
Matrix(const Matrix& t) { memcpy(x, t.x, sizeof(x)); }

Matrix operator*(const Matrix& rhs)
{
Matrix ret;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
ret.x[i][j] = 0;
for (int k = 0; k < N; k++)
ret.x[i][j] += x[i][k] * rhs.x[k][j] % 1007;
ret.x[i][j] %= 1007;
}
return ret;
}
};

Matrix quick_pow(Matrix a, int x)
{
Matrix ret;
for (int i = 0; i < N; i++)
ret.x[i][i] = 1;
while (x)
{
if (x & 1) ret = ret * a;
a = a * a;
x >>= 1;
}
return ret;
}
int main()
{
int T; cin >> T;

Matrix r;
r.x[0][0] = 2;
r.x[0][1] = 0;
r.x[0][2] = 1;

r.x[1][0] = 0;
r.x[1][1] = 2;
r.x[1][2] = 1;

r.x[2][0] = 2;
r.x[2][1] = 2;
r.x[2][2] = 2;
while (T--)
{
int n; cin >> n;
if (n == 1)
{
cout << 2 << endl;
continue;
}
int A = 2, B = 0, C = 2;
Matrix m = quick_pow(r, n - 1);
int* tmp = m.x[0];
int ans = tmp[0] * A + tmp[1] * B + tmp[2] * C;
cout << ans % 10007 << endl;
}
}

E - Q老师度假

Description

Input

Output

Sample Input
3 2
0 1
1 0
4 3
1 2 3
1 2 3
1 2 3
Sample Output
2
9

#include <iostream>
using namespace std;

const int N = 3;
struct Matrix
{
int x[N][N];
Matrix() { memset(x, 0, sizeof(x)); }
Matrix(const Matrix& t) { memcpy(x, t.x, sizeof(x)); }

Matrix operator*(const Matrix& rhs)
{
Matrix ret;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
ret.x[i][j] = 0;
for (int k = 0; k < N; k++)
ret.x[i][j] += x[i][k] * rhs.x[k][j] % 1007;
ret.x[i][j] %= 1007;
}
return ret;
}
};

Matrix quick_pow(Matrix a, int x)
{
Matrix ret;
for (int i = 0; i < N; i++)
ret.x[i][i] = 1;
while (x)
{
if (x & 1) ret = ret * a;
a = a * a;
x >>= 1;
}
return ret;
}
int main()
{
int T; cin >> T;

Matrix r;
r.x[0][0] = 2;
r.x[0][1] = 0;
r.x[0][2] = 1;

r.x[1][0] = 0;
r.x[1][1] = 2;
r.x[1][2] = 1;

r.x[2][0] = 2;
r.x[2][1] = 2;
r.x[2][2] = 2;
while (T--)
{
int n; cin >> n;
if (n == 1)
{
cout << 2 << endl;
continue;
}
int A = 2, B = 0, C = 2;
Matrix m = quick_pow(r, n - 1);
int* tmp = m.x[0];
int ans = tmp[0] * A + tmp[1] * B + tmp[2] * C;
cout << ans % 10007 << endl;
}
}

05-14 81

04-21 63

04-13 63

04-23 38

05-11 67

05-21 46

05-21 150

05-24 27

04-22 77

05-11 70

02-29 146

03-18 128

03-31 36

05-22 59

05-20 29

05-22 16

05-22 58

05-23 87

05-11 46

05-02 20

03-23 17

03-30 31

03-18 14

05-10 10

04-14 58万+

03-13 14万+

03-01 13万+

03-04 13万+

03-08 4万+

03-08 7万+

04-25 6万+

03-10 13万+

03-12 11万+

03-13 11万+

03-14 1万+

03-18 9400

03-19 3万+

03-19 8万+

03-19 3万+

03-22 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-27 1万+

03-29 21万+

03-29 10万+

03-30 16万+

05-28 5763

05-28 1万+

04-02 4043

04-02 4万+

04-06 7万+

04-09 8万+

04-09 2万+

05-17 8105

04-11 3万+

04-15 6万+

04-18 4万+

04-20 4万+

04-24 3万+

04-29 6841

04-30 9801

04-30 9364

05-01 5622

05-03 9945

05-16 5万+

05-06 1万+

05-08 4万+

05-10 3108

05-14 6174

05-14 1817

05-16 3424

05-16 1万+

05-20 833

05-18 8142

刚去面试现场聊了一个多小时的Redis ，悄悄分享给大家！

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客