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

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

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

题目描述:你有一个有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 = 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);
    }
}  

原文地址:https://www.cnblogs.com/caiyishuai/p/9077211.html

时间: 2024-10-04 01:14:58

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

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/7398272.html 先考虑dp求01串的不同子序列的个数. dp[i][j]表示用前i个字符组成的以j为结尾的01串个数. 如果第i个字符为0,则dp[i][0] = dp[i-1][1] + dp[i-1][0] + 1,dp[i][1] = dp[i-1][1] 如果第i个字符为1,则dp[i][1

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

Building Shops Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 701 Accepted Submission(s): 265 Problem Description HDU's n classrooms are on a line ,which can be considered as a number line. Eac

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

Deleting Edges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 567    Accepted Submission(s): 210 Problem Description Little Q is crazy about graph theory, and now he creates a game about grap

2017中国大学生程序设计竞赛 - 女生专场(重现)

A.Automatic Judge(模拟) Problem Description Welcome to HDU to take part in the second CCPC girls’ competition!A new automatic judge system is used for this competition. During the five-hour contest time, you can submit your code to the system, then the

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

Building Shops Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description HDU’s n classrooms are on a line ,which can be considered as a number line. Ea

&quot;巴卡斯杯&quot; 中国大学生程序设计竞赛 - 女生专场(重现)解题思路

此文章可以使用目录功能哟↑(点击上方[+]) 经过这么一次女生赛,告诉我们千万不要小瞧女生,不然会死得很惨,orz... 链接→"巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现)  Problem 1001 Solving Order Accept: 0    Submit: 0 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit : 32768/32768 K (Java/Others)  Problem Descri

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

这是CCPC女生专场的一道dp题.大佬们都说它简单,我并没有感到它有多简单. 先说一下题意:在一条直线上,有n个教室,现在我要在这些教室里从左到右地建设一些作为糖果屋,每个教室都有自己的坐标xi 和建造糖果屋的费用ci , 如果在这里建造一个糖果屋,那么花费ci ,如果不建造糖果屋,则花费是当前教室的坐标与左边最靠近当前教室的糖果屋坐标之差,问最小花费. 一看这是个求最优解的问题,应该明白这是个dp问题,现在来考虑该问题状态的定义: 当我建设到第i个教室的时候,我有两种选择,建糖果屋或者不建糖果

&quot;巴卡斯杯&quot; 中国大学生程序设计竞赛 - 女生专场

Combine String #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<map> using namespace std; typedef unsigned long long LL; #de

&quot;字节跳动杯&quot;2018中国大学生程序设计竞赛-女生专场 Solution

A - 口算训练 题意:询问 $[L, R]$区间内 的所有数的乘积是否是D的倍数 思路:考虑分解质因数 显然,一个数$x > \sqrt{x} 的质因子只有一个$ 那么我们考虑将小于$\sqrt {x}$ 的质因子用线段树维护 其他质因子用vector 维护存在性 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 100010 5 #define block 317 6 vector <int>