码迷,mamicode.com
首页 > 其他好文 > 详细

NEU 1040 Count

时间:2015-11-24 20:31:25      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

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\n",ini.a[0][0]);
65     }
66     return 0;
67 }

 

 

NEU 1040 Count

标签:

原文地址:http://www.cnblogs.com/xuwangzihao/p/4992574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!