(hdu 6030) Happy Necklace 找規律+矩陣快速冪

來源:互聯網
上載者:User

標籤:lease   style   compose   size   acm   script   tle   class   his   

題目連結 :http://acm.hdu.edu.cn/showproblem.php?pid=6030

Problem DescriptionLittle 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}. InputThe 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 integer n(2≤n≤1018), denoting the number of beads on the necklace. OutputFor each test case, print a single line containing a single integer, denoting the answer modulo 109+7. Sample Input223 Sample Output34 Source2017中國大學生程式設計競賽 - 女生專場

題目大意:有n個珠子,有紅藍兩種珠子 符合下列兩個條件的珠子排列方法有多少種?

*******在每串手鏈種,長度為素數的子串種,紅色珠子要不小於藍色珠子

分析:找到規律:f[i] = f[i-1]+f[i-3];因為n很大   可以用矩陣快速冪寫

初始矩陣為2  3  4  轉移矩陣為0  0  1

                                               1   0  0

                                               0   1  1

#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<math.h>#include<queue>#include<stack>#include <vector>#include<iostream>using namespace std;#define N 50005#define INF 0x3f3f3f3f#define LL long long#define mod 1000000007struct node{    LL a[10][10];};node mul(node a,node b){    node te;    memset(te.a,0,sizeof(te.a));    for(int i=0;i<3;i++)    {        for(int j=0;j<3;j++)        {            for(int k=0;k<3;k++)            {                te.a[i][j]+=(a.a[i][k]*b.a[k][j])%mod;            }        }    }    return te;}LL quick(LL n){    if(n<4)    {        int num[]={0,2,3,4};        return num[n];    }    node b,team;    b.a[0][0] = 0;b.a[0][1] = 0;b.a[0][2] = 1;    b.a[1][0] = 1;b.a[1][1] = b.a[1][2] = 0;    b.a[2][0] = 0;b.a[2][1] = b.a[2][2] = 1;    memset(team.a,0,sizeof(team.a));    for(int i=0;i<3;i++)        team.a[i][i] = 1;    LL k = n-3;    while(k)    {        if(k&1)        team = mul(team,b);        b = mul(b,b);        k = k/2;    }    return (team.a[0][2]*2+team.a[1][2]*3+team.a[2][2]*4)%mod;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        LL n;        scanf("%lld",&n);        printf("%lld\n",quick(n));    }    return 0;
}

 

(hdu 6030) Happy Necklace 找規律+矩陣快速冪

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.