HDU 6030 Happy Necklace【矩阵快速幂】
来源:互联网 发布:mac万能辅助在哪 编辑:程序博客网 时间:2024/06/07 14:31
Happy Necklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 477 Accepted Submission(s): 198
Problem Description
Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads.
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 exactlyn 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 109+7 .
Note: The necklace is a single string, {not a circle}.
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
Note: The necklace is a single string, {not a circle}.
Input
The first line of the input contains an integer T(1≤T≤10000) , denoting the number of test cases.
For each test case, there is a single line containing an integern(2≤n≤1018) , denoting the number of beads on the necklace.
For each test case, there is a single line containing an integer
Output
For each test case, print a single line containing a single integer, denoting the answer modulo 109+7 .
Sample Input
223
Sample Output
34
Source
2017中国大学生程序设计竞赛 - 女生专场
因为2是最小的素数,考虑长度为2的子串。红色为A,蓝色为B,则只有AA,AB,BA三种情况。对每种情况,在后面加上A或B,AA可以形成AA,AB,AB可以形成BA,BA可以形成AA。通过这个递推扩展到长度为n的情况,用矩阵快速幂加速即可。矩阵为:
初始情况下,AA,AB,BA都有可能,因此最后将矩阵中的所有数字相加就是答案。
注意n为10的18次方会爆int所以在矩阵快速幂的时候要用long long【Matrix quickpow(Matrix A,ll k)】
#include<iostream>#include<algorithm>#include<cmath>#include<cstdio>#include<cstring>#define INF 0x3f3f3f3f#define mod 1000000007using 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);}}
1018
101Matrix quickpow(Matrix A,ll k)】1018101810181018101810181018101810
1018
1018
1018
1018
阅读全文
0 0
- HDU 6030 Happy Necklace【矩阵快速幂】
- HDU 6030 Happy Necklace【矩阵快速幂】
- HDU 6030 Happy Necklace[找规律][矩阵快速幂]
- HDU 6030 Happy Necklace (递推+矩阵快速幂)
- Hdu 6030 Happy Necklace【Dp+矩阵快速幂】
- hdu 6030 Happy Necklace (递推,矩阵快速幂)
- 2017 CCPC-WFinal&&HDOJ 6030 Happy Necklace(矩阵快速幂)
- HDU6030 Happy Necklace[矩阵快速幂]
- HDU6030 Happy Necklace(矩阵快速幂)
- HDU 6030 Happy Necklace
- hdu 6030 Happy Necklace
- hdu 6030 Happy Necklace
- HDU6030-Happy Necklace-找规律+矩阵快速幂
- 【HDU6030】Happy Necklace(找规律+矩阵快速幂)
- HDU 6030 Happy Necklace (DP)
- hdoj 6030 Happy Necklace
- 2017CCPC女生赛 hdu 6030 Happy Necklace
- So happy!(get 矩阵快速幂)ICPC :E HDU 6198
- Kotlin的Spring之旅(一):使用IDEA搭建Gradle+Kotlin+Spring项目
- SQL中查找当天、本月、本年的数据
- AWS S3上传文件,并获取公用的下载链接
- ubuntu下打造C/C++开发环境(Eclipse)
- 技术开发心得
- HDU 6030 Happy Necklace【矩阵快速幂】
- java基础关系操作符,==/!=
- Docker Compose—简化复杂容器应用的利器
- Java 8 Optional类深度解析
- Python函数中,参数前*的作用
- 线程安全与可重入函数
- Linux Command Line and....ch14(处理用户输入)
- JSP(1)
- 主成分分析、聚类分析、因子分析的基本思想及优缺点