HDU 6030 Happy Necklace

Source: Internet
Author: User

Title: http://acm.hdu.edu.cn/showproblem.php?pid=6030

Test instructions: Give the red and blue two, and then into a string, it is required in each length of the interval is the number of R (red) is not small and the number of B (blue);

Solution: The difficulty lies in how to find the law. It is easy to infer that if a string of length 2 or 3 satisfies r>=b, then the prime number (5, 7 ...) will satisfy the r>=b.

Suppose that there is now a string of n characters with a scheme of f (N), considering whether F (N) can be pushed from F (n-1)?

Case 1. If the last character of the string is R, then the second-to-penultimate can be either R or B. So at this point f (n) = f (n-1).

Scenario 2. If the last character of the string is B, then the second-to-bottom must be r to meet the "length of 2 and r>=b" condition, then the third-to-bottom must be r, otherwise it does not meet the "length 3 and r>=b" condition. So at this point f (n) = f (n-3)

In conclusion, f (n) = f (n-1) + f (n-3) can be used.

Because of the huge amount of input (1018), it is not feasible to use the array to store recursive results, and each recursion is too slow, so it can only be solved by the matrix fast power.

Since f (n-3) appears in the recursive type, the transfer matrix must be 3*3.

The transitive matrix is deduced to be

1 1 0

0 0 1

1 0 0

AC:

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<vector>using namespaceStd;typedefLong Longll;Constll m =1000000000+7;Const intN =4;structmar{ll a[n][n];};//structured matricesMar mul (Mar X,mar y) {Mar temp;  for(inti =1; i < N; i++)         for(intj =1; J < N; J + +) Temp.a[i][j]=0;  for(inti =1; i < N; i++)         for(intj =1; J < N; J + +)                 for(intK =1; K < N; k++) Temp.a[i][j]+ = (x.a[i][k]%m*y.a[k][j]%m)%m; returntemp;}//matrix multiplicationMar quickpow (Mar a,ll N) {Mar 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;//initialize to the unit matrix     while(n) {if(n&1) res =Mul (res,a); A=Mul (a,a); N>>=1; }    returnRes;}//Matrix Fast Power Core codeintMain () {intT;    ll N; CIN>>T;  while(t--) {cin>>N; if(n==2) {cout <<3<<endl;Continue;}        Mar 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;//construct the transfer matrixMar res = QUICKPOW (a,n-2); cout<< (res.a[1][1] + res.a[1][2] + res.a[1][3])% m+ (res.a[2][1] + res.a[2][2] + res.a[2][3])% m+ (res.a[3][1] + res.a[3][2] + res.a[3][3]) (% m)%m <<Endl; }    return 0;}

HDU 6030 Happy Necklace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.