2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)
Posted 蔡军帅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)相关的知识,希望对你有一定的参考价值。
Happy Necklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1146 Accepted Submission(s): 491
Little Q desperately wants to impress his girlfriend, he knows that she will like the necklace only if for every prime length continuous subsequence in the necklace, the number of red beads is not less than the number of blue beads.
Now Little Q wants to buy a necklace with exactly beads. He wants to know the number of different necklaces that can make his girlfriend happy. Please write a program to help Little Q. Since the answer may be very large, please print the answer modulo .
Note: The necklace is a single string, {not a circle}.
For each test case, there is a single line containing an integer , denoting the number of beads on the necklace.
如果用a表示红色,用b表示蓝色。题意明显可以看出只需要管长度2和3的连续序列是否符合!
如果以b结尾,那么下一个必须是a,或者加个aab就可以了!
先看这个特征方程F[i] = F[i - 1] + F[i - 3],那么就有一个矩阵如下
我们的目标矩阵就是
那么,针对这个矩阵我们如何转置呢?
先看目标矩阵第一个:F[i]
F[i] = F[i - 1] + F[i - 3]
那么,由矩阵乘法,转置矩阵第一行,似乎就定了:1 0 1
同样的,二三行就是1 0 0 和 0 1 0
整个矩阵如下:
#include<iostream> #include<algorithm> #include<cmath> #include<cstdio> #include<cstring> #define INF 0x3f3f3f3f #define mod 1000000007 using namespace std; typedef long long ll; const int maxn = 100010; ll n; struct Matrix { ll a[5][5]; }; Matrix mul(Matrix x, Matrix y) { Matrix temp; for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) temp.a[i][j] = 0; for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { ll sum = 0; for (int k = 1; k <= 3; k++) { sum = (sum + x.a[i][k] * y.a[k][j] % mod) % mod; } temp.a[i][j] = sum; } } return temp; } Matrix quickpow(Matrix A,ll k) { Matrix res; res.a[1][1] = 1; res.a[1][2] = 0; res.a[1][3] = 0; res.a[2][1] = 0; res.a[2][2] = 1; res.a[2][3] = 0; res.a[3][1] = 0; res.a[3][2] = 0; res.a[3][3] = 1; while (k) { if (k & 1) res = mul(res, A); A = mul(A, A); k >>= 1; } return res; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%lld", &n); if (n == 2) { printf("3\\n"); continue; } Matrix A; A.a[1][1] = 1; A.a[1][2] = 0; A.a[1][3] = 1; A.a[2][1] = 1; A.a[2][2] = 0; A.a[2][3] = 0; A.a[3][1] = 0; A.a[3][2] = 1; A.a[3][3] = 0; Matrix res = quickpow(A, n - 2); ll x = (res.a[1][1] + res.a[1][2] + res.a[1][3]) % mod; ll y = (res.a[2][1] + res.a[2][2] + res.a[2][3]) % mod; ll z = (res.a[3][1] + res.a[3][2] + res.a[3][3]) % mod; printf("%lld\\n", (x + y + z) % mod); } }
以上是关于2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)的主要内容,如果未能解决你的问题,请参考以下文章
2017中国大学生程序设计竞赛 - 女生专场 1002 dp
2017中国大学生程序设计竞赛 - 女生专场(Graph Theory)
2017中国大学生程序设计竞赛 - 女生专场 Deleting Edges(思维+最短路)
2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)