hdu_女生赛_6030递推+矩阵
来源:互联网 发布:货运单打印软件 编辑:程序博客网 时间:2024/06/05 05:20
Happy Necklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 253 Accepted Submission(s): 99
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中国大学生程序设计竞赛 - 女生专场
递推式推出来 然后矩阵快速幂搞一搞
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #include <cmath> #include <cstdlib> typedef long long ll; const int N = 0; const int SIZE = 3; int l, MOD=1e9+7; struct Mat{ ll v[SIZE][SIZE]; // value of matrix Mat() { memset(v, 0, sizeof(v)); } }; Mat operator * (Mat a, Mat b) { Mat c; for(int i=0;i<SIZE;i++) { for(int j=0;j<SIZE;j++) { c.v[i][j] = 0; for(int k=0;k<SIZE;k++){ c.v[i][j] += (a.v[i][k] * b.v[k][j]) % MOD; c.v[i][j] %= MOD; } } } return c; } Mat mul(Mat a,ll k ){Mat tmp;int i; for(i=0;i<SIZE;i++){tmp.v[i][i]=1;} while(k){if(k&1){tmp=(tmp*a);}a=a*a;k>>=1;}return tmp;}int main() { int t; scanf("%d",&t); while (t--){ Mat a, b, c; // a a.v[0][0] = 4; a.v[1][0] = 3;a.v[2][0] = 2; // b b.v[0][0] = b.v[0][2]=b.v[1][0]=b.v[2][1]=1; //初始化地推矩阵 ll n; scanf("%lld",&n); if(n==2){ printf("3\n"); } else if(n==3){ printf("4\n"); } else { c=mul(b,n-3); c=c*a; printf("%lld\n",c.v[0][0]); } } return 0; }
0 0
- hdu_女生赛_6030递推+矩阵
- 递推---分数矩阵
- 递推+矩阵快速幂
- 矩阵快速幂+递推
- POJ2663(矩阵+递推)
- nyoj 301 递推求值(矩阵递推)
- NYOJ - 301 - 递推求值 ( 递推+矩阵快速幂 )
- 【递推&矩阵快速幂】hdu 2604
- HDU1005 一类递推矩阵优化
- hdu 2604 Queuing(矩阵乘法+递推)
- hdu 3306 矩阵解决线性递推
- 用矩阵表示数列递推公式
- hdu2604(递推,矩阵快速幂)
- hdu2971 递推+矩阵快速幂
- UVa11464偶数矩阵(部分枚举+递推)
- uva 10870 Recurrences(矩阵优化递推)
- hdu 2065(递推+矩阵乘法)
- 矩阵乘法递推的优化艺术
- C语言大作业
- 解决某些有到期时间的场景,不适合定时扫描表来完成处理的情况
- 算法设计Week12 LeetCode Algorithms Problem #63 Unique Paths II
- Lattice Diamond与modelsim联合仿真环境设置
- MyBatis 模糊查询 like %
- hdu_女生赛_6030递推+矩阵
- 用Metasploit破解ftp用户名和密码
- Huffman树与Huffman编码
- error: macro names must be identifiers
- sql语句中----删除表数据drop、truncate和delete的用法
- [数据结构]魔王语言解释 c语言实现
- 关于数据库的随笔
- JAVA JDBC连接 SQLServer2012
- 前端调用微信支付接口