zoukankan      html  css  js  c++  java
  • NEU 1040 Count

    1040: Count

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 59  解决: 23
    [提交][状态][讨论版]

    题目描述

    Many ACM team name may be very funny,such as "Complier_Error","VVVVV".Oh,wait for a minute here.

    Is it "W W"+"V",or "W"+"V V V",or something others we can treat as?There are several ways we can treat this name "VVVVV" (5 'V's),as V V can be treat as a W.

    For 5 'V's,our have 8 ways.They are:

    1. V V V V V

    2. V W W

    3. W W V

    4. V W V V

    5. W V W

    6. W V V V

    7. V V W V

    8. V V V W

    The problem here is that for n 'V's,how many ways do we have to treat it?Because the answer may be too large, you should output the answer module by p.(If n is 0,then we have just one way.)

    输入

    There are multiple test cases. The first line of the input contains an integer M, meaning the number of the test cases.
    For each test cases, there are two integers n and p in a single line.
    You can assume that 0<=n<=21000000000<p<=2009.

    输出

    For each test case, output the answer with case number in a single line.

    样例输入

    2
    5 5
    4 7

    样例输出

    3
    5


    题目大意:
    就是说给你5个V的话“VVVVV”,可能有一部分V连在一起被看做W,问可以看出多少种序列,答案对p取余。
    输入:
    第一行一个M表示有M个测试数据,
    第二行到第M+1行每行两个整数n,p,表示n个V,对p取余。
    输出:
    一个整数,要求如题。

    菲波那切数列??应该吧

    好吧,就是斐波那契额,n的数目由n-1和n-2继承来,n比较大,所以矩阵乘法快速幂优化一下递推

     1 #include<cstdio>
     2 #include<cstring>
     3 int p;
     4 struct matrix
     5 {
     6     int a[2][2];
     7     matrix(matrix &p)
     8     {
     9         for(int i=0;i<2;i++)
    10             for(int j=0;j<2;j++)
    11                 this->a[i][j]=p.a[i][j];
    12     }
    13     matrix(int x)
    14     {
    15         for(int i=0;i<2;i++)
    16             for(int j=0;j<2;j++)
    17                 this->a[i][j]=x;
    18     }
    19     matrix()
    20     {
    21         memset(a,0,sizeof(a));
    22         for(int j=0;j<2;j++)
    23             this->a[j][j]=1;
    24     }
    25     matrix operator * (matrix &b)
    26     {
    27         matrix c;
    28         for(int i=0;i<2;i++)
    29             for(int j=0;j<2;j++)
    30             {
    31                 c.a[i][j]=0;
    32                 for(int k=0;k<2;k++)
    33                 {
    34                     c.a[i][j]+=this->a[i][k]*b.a[k][j];
    35                 }
    36                 c.a[i][j]%=p;
    37             }
    38         return c;
    39     }
    40 };
    41 matrix quickmult(matrix &a,int k)
    42 {
    43     matrix ans,temp(a);
    44     while(k)
    45     {
    46         if(k%2)ans=ans*temp;
    47         temp=temp*temp;
    48         k/=2;
    49     }
    50     return ans;
    51 }
    52 int main()
    53 {
    54     int m,n;
    55     scanf("%d",&m);
    56     while(m--)
    57     {
    58         scanf("%d%d",&n,&p);
    59         matrix ini,tra;//ini means initial matrix, tra means transform matrix
    60         ini.a[0][0]=1;ini.a[0][1]=1;ini.a[1][0]=0;ini.a[1][1]=0;
    61         tra.a[0][0]=0;tra.a[0][1]=1;tra.a[1][0]=1;tra.a[1][1]=1;
    62         tra=quickmult(tra,n);
    63         ini=ini*tra;
    64         printf("%d
    ",ini.a[0][0]);
    65     }
    66     return 0;
    67 }
     
  • 相关阅读:
    OpenCV图像处理之 Mat 介绍
    linux 更改网卡名称 eth0
    【git】git常用命令
    【JS】函数提升变量提升以及函数声明和函数表达式的区别
    【VUE】vue中遍历数组和对象
    加密盐的意义和用途
    sql server2005版本中,len函数计算了字符串末尾的空格
    ES之一:API使用及常用概念
    flink (一)
    ClassLoader详解 (JDK9以前)
  • 原文地址:https://www.cnblogs.com/xuwangzihao/p/4992574.html
Copyright © 2011-2022 走看看