Lucky Coins Sequence

柔光的暖阳◎ 2021-12-24 17:23 321阅读 0赞







































Lucky Coins Sequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35 Accepted Submission(s): 29
 


Problem Description


As we all know,every coin has two sides,with one side facing up and another side facing down.Now,We consider two coins’s state is same if they both facing up or down.If we have N coins and put them in a line,all of us know that it will be 2^N different ways.We call a “N coins sequence” as a Lucky Coins Sequence only if there exists more than two continuous coins’s state are same.How many different Lucky Coins Sequences exist?


 


Input


There will be sevaral test cases.For each test case,the first line is only a positive integer n,which means n coins put in a line.Also,n not exceed 10^9.


 


Output


You should output the ways of lucky coins sequences exist with n coins ,but the answer will be very large,so you just output the answer module 10007.


 


Sample Input


  1. 3
    4



 


Sample Output


  1. 2
    6



 
 


Source


2010 ACM-ICPC Multi-University Training Contest(9)——Host by HNU


 


Recommend


zhengfeng


 
  1. /*
  2. 题意:定义一个硬币序列,如果超过三个相邻的硬币的正反相同,那么这个序列就是Lucky序列,给出序列
  3. 长度n,问有几种Lucky序列
  4. 初步思路:这种题型一般都是递推过来的,不管了先打表试一下
  5. 打表结果:(从3开始的)
  6. 2
  7. 6
  8. 16
  9. 38
  10. 86
  11. 188
  12. 402
  13. 846
  14. 1760
  15. 3630
  16. 7438
  17. 15164
  18. 30794
  19. 62342
  20. 125904
  21. 253782
  22. 510758
  23. 1026684
  24. 每项和前项的二倍差的数刚好是一个变形的斐波那契数列。
  25. 得到递推公式:G(n)=2*(G(n-1)+F(n-2));
  26. #感悟:一边AC爽
  27. */
  28. #include<bits/stdc++.h>
  29. #define mod 10007
  30. using namespace std;
  31. /********************************矩阵模板**********************************/
  32. class Matrix {
  33. public:
  34. int a[3][3];
  35. void init(int x) {
  36. memset(a,0,sizeof(a));
  37. if(x==1){
  38. a[0][0]=2;
  39. a[0][1]=1;
  40. a[0][2]=1;
  41. }else{
  42. a[0][0]=2;
  43. a[1][0]=2;
  44. a[1][1]=1;
  45. a[1][2]=1;
  46. a[2][1]=1;
  47. }
  48. }
  49. Matrix operator +(Matrix b) {
  50. Matrix c;
  51. for (int i = 0; i < 3; i++)
  52. for (int j = 0; j < 3; j++)
  53. c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
  54. return c;
  55. }
  56. Matrix operator +(int x) {
  57. Matrix c = *this;
  58. for (int i = 0; i < 3; i++)
  59. c.a[i][i] += x;
  60. return c;
  61. }
  62. Matrix operator *(Matrix b)
  63. {
  64. Matrix p;
  65. memset(p.a,0,sizeof p.a);
  66. for (int i = 0; i < 3; i++)
  67. for (int j = 0; j < 3; j++)
  68. for (int k = 0; k < 3; k++)
  69. p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
  70. return p;
  71. }
  72. Matrix power_1(int t) {
  73. Matrix ans,p = *this;
  74. memset(ans.a,0,sizeof ans.a);
  75. for(int i=0;i<3;i++) ans.a[i][i]=1;
  76. while (t) {
  77. if (t & 1)
  78. ans=ans*p;
  79. p = p*p;
  80. t >>= 1;
  81. }
  82. return ans;
  83. }
  84. Matrix power_2(Matrix a,Matrix b,int x){
  85. while(x){
  86. if(x&1){
  87. b=a*b;
  88. }
  89. a=a*a;
  90. x>>=1;
  91. }
  92. return b;
  93. }
  94. }unit,init;
  95. /********************************矩阵模板**********************************/
  96. int n;
  97. int main(){
  98. // freopen("in.txt","r",stdin);
  99. while(scanf("%d",&n)!=EOF){
  100. if(n<3){
  101. printf("0\n");
  102. continue;
  103. }
  104. if(n==3){
  105. printf("2\n");
  106. continue;
  107. }
  108. unit.init(1);
  109. init.init(0);
  110. // for(int i=0;i<3;i++){
  111. // for(int j=0;j<3;j++){
  112. // cout<<unit.a[i][j]<<" ";
  113. // }
  114. // cout<<endl;
  115. // }
  116. init=init.power_1(n-3);
  117. printf("%d\n",(unit*init).a[0][0]);
  118. }
  119. return 0;
  120. }

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/6388559.html

发表评论

表情:
评论列表 (有 0 条评论,321人围观)

还没有评论,来说两句吧...

相关阅读