HDU 6030 Happy Necklace

矩阵快速幂。

#include <bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
const long long inf=1e18;

int T;
long long n;

struct Matrix
{
    long long A[5][5];
    int R, C;
    Matrix operator*(Matrix b);
};

Matrix X, Y, Z;

Matrix Matrix::operator*(Matrix b)
{
    Matrix c;
    memset(c.A, 0, sizeof(c.A));
    int i, j, k;
    for (i = 1; i <= R; i++)
        for (j = 1; j <= C; j++)
            for (k = 1; k <= C; k++)
                c.A[i][j] = (c.A[i][j] + (A[i][k] * b.A[k][j])%mod)%mod;
    c.R=R; c.C=b.C;
    return c;
}

void init()
{
    n = n - 2;
    memset(X.A,0,sizeof X.A);
    memset(Y.A,0,sizeof Y.A);
    memset(Z.A,0,sizeof Z.A);

    Z.R = 1; Z.C = 3;
    Z.A[1][1]=1; Z.A[1][2]=1; Z.A[1][3]=1;

    for(int i=1;i<=3;i++) Y.A[i][i]=1; Y.R = 3; Y.C = 3;

    X.A[1][1]=1; X.A[1][2]=0; X.A[1][3]=1;
    X.A[2][1]=1; X.A[2][2]=0; X.A[2][3]=0;
    X.A[3][1]=0; X.A[3][2]=1; X.A[3][3]=0;

    X.R = 3; X.C = 3;
}

void work()
{
    while (n)
    {
        if (n % 2 == 1) Y = Y*X;
        n = n >> 1;
        X = X*X;
    }
    Z = Z*Y;

    printf("%lld\n", (Z.A[1][1]+Z.A[1][2]+Z.A[1][3])%mod);
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&n);
        init();
        work();
    }
    return 0;
}
时间: 2024-10-14 11:12:43

HDU 6030 Happy Necklace的相关文章

hdu 2660 Accepted Necklace (二维背包)

Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2530    Accepted Submission(s): 999 Problem Description I have N precious stones, and plan to use K of them to make a necklace f

(hdu 6030) Happy Necklace 找规律+矩阵快速幂

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 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,

hdu 2660 Accepted Necklace(01-背包变形 || DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2660 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

hdu - 2660 Accepted Necklace (二维费用的背包问题)

http://acm.hdu.edu.cn/showproblem.php?pid=2660 f[v][u]=max(f[v][u],f[v-1][u-w[i]]+v[i]; 注意中间一层必须逆序循环. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 int dp[21][1001]; 6 int v[1001],w[1001]; 7 int

HDU 5730 Shell Necklace(CDQ分治+FFT)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5730 [题目大意] 给出一个数组w,表示不同长度的字段的权值,比如w[3]=5表示如果字段长度为3,则其权值为5,现在有长度为n的字段,求通过不同拆分得到的字段权值乘积和. [题解] 记DP[i]表示长度为i时候的答案,DP[i]=sum_{j=0}^{i-1}DP[j]w[i-j],发现是一个卷积的式子,因此运算过程可以用FFT优化,但是由于在计算过程中DP[j]是未知值,顺次计算复杂度是O(

HDU 2660 Accepted Necklace (DFS)

Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2474    Accepted Submission(s): 973 Problem Description I have N precious stones, and plan to use K of them to make a necklace f

hdu 5730 Shell Necklace —— 分治FFT

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5730 DP式:\( f[i] = \sum\limits_{j=1}^{i} f[i-j] * a[j] \) 因为没有给 \( f[0] \) 赋初值,所以在递归底层令 \( f[l] += a[l] \) 注意多组数据清空数组: 读入 \( s[i] \) 时要取模!! 代码如下: #include<iostream> #include<cstdio> #include<cstr

HDU 5730 - Shell Necklace

题意: 给出连续的1-n个珠子的涂色方法 a[i](1<=i<=n), 问长度为n的珠链共有多少种涂色方案 分析: 可以得到DP方程: DP[n] = ∑(i=1,n) (DP[n-i]*a[i]). 该方程为卷积形式,故 CDQ + FFT CDQ: 将 [l,r] 二分, 先得到[l,mid]的答案,再更新[l,mid]对[mid+1,r]的贡献.   对任意 DP[j](mid+1 <= j <= r), [l,mid] 对其贡献为 ∑(i=l,mid) (DP[i]*a[j

hdu 6030

题意: 给出红蓝两种,然后排成一个字符串,要求在每一个长度为素数的区间里面是的r(red)的数量不小与b(blue)的数量; 思路:想象当n为2的时候的情况是 rr,rb,br,三种情况,当n为3的时候相当于在后面添加一个b或者r,会发现形成rr的情况是前面rr和br的和,形成br的情况是前面的rb,而形成rb的情况是前面的rr,不能有前面的br形成rb,因为在素数为3的时候不能形成brb: 所以你会发现这个针对的素数只是2和3: 根据递推,设数组a[],b[],c[]分别为后面两个字母为rr,