Happy Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1146    Accepted Submission(s): 491

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 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}.
 
Input
The first line of the input contains an integer , denoting the number of test cases.
For each test case, there is a single line containing an integer , denoting the number of beads on the necklace.
 
Output
For each test case, print a single line containing a single integer, denoting the answer modulo .
 
Sample Input

223
 
Sample Output

34
 
Source
题目描述:你有一个有n个颜色为红色或蓝色的珠子的项链,你可以将项链截断成长度为素数的珠子,如果截取出的一段红色的珠子的个数大于蓝色的珠子,则成为good,问一共有多少种good的可能性。
    可以发现
    n=2,res=3;n=3,res=4;n=4,res=6;n=5,res=9;
    故有递推式 An=An-1+An-3;

如果用a表示红色,用b表示蓝色。题意明显可以看出只需要管长度2和3的连续序列是否符合!

如果以b结尾,那么下一个必须是a,或者加个aab就可以了!

    所以同理就可以推出递推式An=An-1+An-3;
    看一下题目的数据范围,n最大1e18,因此常规O(n)的递推显然不可行,因此直接上O(logn)的矩阵快速幂。
    发现递推式是四阶的递推式,故所得的常数矩阵应该是四维的。之后只需带入矩阵快速幂模板即可。

先看这个特征方程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 = ; ll n; struct Matrix {
ll a[][];
}; Matrix mul(Matrix x, Matrix y)
{
Matrix temp;
for (int i = ; i <= ; i++)
for (int j = ; j <= ; j++) temp.a[i][j] = ; for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
ll sum = ;
for (int k = ; k <= ; 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[][] = ; res.a[][] = ; res.a[][] = ;
res.a[][] = ; res.a[][] = ; res.a[][] = ;
res.a[][] = ; res.a[][] = ; res.a[][] = ;
while (k)
{
if (k & ) res = mul(res, A);
A = mul(A, A);
k >>= ;
}
return res;
} int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%lld", &n);
if (n == )
{
printf("3\n");
continue;
}
Matrix A;
A.a[][] = ; A.a[][] = ; A.a[][] = ;
A.a[][] = ; A.a[][] = ; A.a[][] = ;
A.a[][] = ; A.a[][] = ; A.a[][] = ;
Matrix res = quickpow(A, n - );
ll x = (res.a[][] + res.a[][] + res.a[][]) % mod;
ll y = (res.a[][] + res.a[][] + res.a[][]) % mod;
ll z = (res.a[][] + res.a[][] + res.a[][]) % mod;
printf("%lld\n", (x + y + z) % mod);
}
}

2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)的更多相关文章

  1. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...

  2. 2017中国大学生程序设计竞赛 - 女生专场 Deleting Edges(思维+最短路)

    Deleting Edges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  3. 2017中国大学生程序设计竞赛 - 女生专场(Graph Theory)

    Graph Theory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  4. 2017中国大学生程序设计竞赛 - 女生专场(dp)

    Building Shops Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...

  5. 2017中国大学生程序设计竞赛 - 女生专场 1002 dp

    Building Shops Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  6. 2017中国大学生程序设计竞赛 - 女生专场C【前后缀GCD】

    C HDU - 6025 [题意]:去除数列中的一个数字,使去除后的数列中所有数字的gcd尽可能大. [分析]: 数组prefixgcd[],对于prefixgcd[i]=g,g为a[0]-a[i]的 ...

  7. 2017中国大学生程序设计竞赛 - 女生专场B【DP】

    B HDU - 6024 [题意]:n个教室,选一些教室建造糖果商店. 每个教室,有一个坐标xi和在这个教室建造糖果商店的花费ci. 对于每一个教室,如果这个教室建造糖果商店,花费就是ci,否则就是与 ...

  8. 2017中国大学生程序设计竞赛 - 女生专场A【模拟】

    A HDU - 6023 [题意]:求AC题数和总时长. [分析]:模拟.设置标记数组记录AC与否,再设置错题数组记录错的次数.罚时罚在该题上,该题没AC则不计入总时间,AC则计入.已经AC的题不用再 ...

  9. HDU 6024(中国大学生程序设计竞赛女生专场1002)

    这是CCPC女生专场的一道dp题.大佬们都说它简单,我并没有感到它有多简单. 先说一下题意:在一条直线上,有n个教室,现在我要在这些教室里从左到右地建设一些作为糖果屋,每个教室都有自己的坐标xi 和建 ...

随机推荐

  1. node基础(一)——http模块

    一.http模块 http.createSverver() http是node自带的模块,通过require("http")的方法载入: 使用http创建服务器: http.cre ...

  2. redis key 通配符 查询相应的key

    keys pattern 查询相应的key 在redis里,允许模糊查询key 有3个通配符 *, ? ,[] *: 通配任意多个字符 ?: 通配单个字符 []: 通配括号内的某1个字符 redis  ...

  3. 【C#基本功 控件的用法】 委托

    接触C#这段时间,很多内容容易理解,但也也有很多内容总是无法踏过门槛,就像Delegate 委托,这种新的机制是Labview不具备的,他的一个用法,也让我们这些从labview跨越过来的coder, ...

  4. Codeforces Round #451 (Div. 2)

    水题场.... 结果因为D题看错题意,B题手贱写残了...现场只出了A,C,E A:水题.. #include<bits/stdc++.h> #define fi first #defin ...

  5. Dubbo定义及其作用

    Dubbo定义 Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只 ...

  6. C++多态、虚函数、纯虚函数、抽象类

    多态 同一函数调用形式(调用形式形同)可以实现不同的操作(执行路径不同),就叫多态. 两种多态: (1)静态多态:分为函数重载和运算符重载,编译时系统就能决定调用哪个函数. (2)动态多态(简称多态) ...

  7. python匿名函数 与 内置函数

      一.匿名函数  1.定义: 匿名函数顾名思义就是指:是指一类无需定义标识符(函数名)的函数或子程序. 2.语法格式:lambda 参数:表达式 lambda语句中,开头先写关键字lambda,冒号 ...

  8. set, map, string, find(), string name[100],等的混合

    Unrequited Love Time Limit: 16 Seconds      Memory Limit: 131072 KB There are n single boys and m si ...

  9. windows server 2016 docker 之创建使用虚拟交换机

    windows server 2016 Create a virtual switch for Hyper-V virtual machines 操作步骤: 服务器只有一块网卡连接了网络 尝试1: h ...

  10. jQueryValidation插件API 学习

    一般格式: $('').viladata({ rules:{ username:{ required:true, maxlength:2, minlength:10, remote:{ url:&qu ...