zoukankan      html  css  js  c++  java
  • Happy Necklace(矩阵快速幂)

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

    Total Submission(s): 222    Accepted Submission(s): 91


    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 n 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}.

     

    Input

    The first line of the input contains an integer T(1T10000), denoting the number of test cases.
    For each test case, there is a single line containing an integer n(2n1018), 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 109+7.

     

    Sample Input

    2
    2
    3

     

    Sample Output

    3

    4

    //题意:给出红蓝两种珠子,要做一条长为 n 个珠子的项链,要求对于每一个素数长度的项链,都要使红色大于等于蓝色,问有多少种搭配方法

    找出规律后,就知道只是简单的矩阵快速幂了,f[n] = f[n-1] + f[n-3]

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 #define LL long long
     6 #define MOD 1000000007
     7 struct Mat
     8 {
     9     LL m[3][3];
    10 }unit,base;
    11 LL n;
    12 
    13 Mat Mult(Mat a,Mat b)
    14 {
    15     Mat re;
    16     for (int i=0;i<3;i++)
    17     {
    18         for (int j=0;j<3;j++)
    19         {
    20             re.m[i][j]=0;
    21             for (int k=0;k<3;k++)
    22                 re.m[i][j]=(re.m[i][j]+ a.m[i][k]*b.m[k][j])%MOD;
    23         }
    24     }
    25     return re;
    26 }
    27 
    28 void Init()
    29 {
    30     base.m[0][0]=1,base.m[0][1]=0,base.m[0][2]=1;
    31     base.m[1][0]=1,base.m[1][1]=0,base.m[1][2]=0;
    32     base.m[2][0]=0,base.m[2][1]=1,base.m[2][2]=0;
    33     memset(unit.m,0,sizeof(unit.m));
    34     for (int i=0;i<3;i++) unit.m[i][i]=1;
    35 }
    36 
    37 LL cal(LL n)
    38 {
    39     if (n<4)    //n太小的情况
    40     {
    41         LL num[4]={0,2,3,4};
    42         return num[n];
    43     }
    44     Mat s=unit,b=base;
    45     LL x = n-3;
    46     while(x)
    47     {
    48         if (x&1) s = Mult(s,b);
    49         b = Mult(b,b);
    50         x/=2;
    51     }
    52     return (s.m[0][0]*4+s.m[0][1]*3+s.m[0][2]*2)%MOD;
    53 }
    54 
    55 int main()
    56 {
    57     int T;
    58     scanf("%d",&T);
    59     while (T--)
    60     {
    61         scanf("%lld",&n);
    62         Init();
    63         printf("%lld
    ",cal(n));
    64     }
    65     return 0;
    66 }
    View Code
  • 相关阅读:
    「CF505E」 Mr. Kitayuta vs. Bamboos
    「CF1438D」 Powerful Ksenia
    Kruskal重构树
    20210528模拟赛总结
    20210527模拟赛总结
    20210526模拟赛总结
    20210525模拟赛总结
    CF #722 Div2题解
    洛谷P3652 csh和zzy的战争 题解
    [清华集训2012]模积和 题解
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6832663.html
Copyright © 2011-2022 走看看