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<=2100000000, 0<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比较大,所以矩阵乘法快速幂优化一下递推
 #include<cstdio>
#include<cstring>
int p;
struct matrix
{
int a[][];
matrix(matrix &p)
{
for(int i=;i<;i++)
for(int j=;j<;j++)
this->a[i][j]=p.a[i][j];
}
matrix(int x)
{
for(int i=;i<;i++)
for(int j=;j<;j++)
this->a[i][j]=x;
}
matrix()
{
memset(a,,sizeof(a));
for(int j=;j<;j++)
this->a[j][j]=;
}
matrix operator * (matrix &b)
{
matrix c;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
c.a[i][j]=;
for(int k=;k<;k++)
{
c.a[i][j]+=this->a[i][k]*b.a[k][j];
}
c.a[i][j]%=p;
}
return c;
}
};
matrix quickmult(matrix &a,int k)
{
matrix ans,temp(a);
while(k)
{
if(k%)ans=ans*temp;
temp=temp*temp;
k/=;
}
return ans;
}
int main()
{
int m,n;
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&n,&p);
matrix ini,tra;//ini means initial matrix, tra means transform matrix
ini.a[][]=;ini.a[][]=;ini.a[][]=;ini.a[][]=;
tra.a[][]=;tra.a[][]=;tra.a[][]=;tra.a[][]=;
tra=quickmult(tra,n);
ini=ini*tra;
printf("%d\n",ini.a[][]);
}
return ;
}
 

最新文章

  1. Redis五种基本数据结构
  2. docker pull certification error
  3. Vue中class与style绑定
  4. ImageMagick jmagick 安装
  5. PHP 全局变量 $_SERVER
  6. css3之圆角效果 border-radius
  7. 【bzoj1004】[HNOI2008]Cards
  8. Linux驱动编程--基于I2C子系统的I2C驱动的Makefile
  9. CSS定位与浮动
  10. openstack 网络简史
  11. IntelliJ Idea 常用快捷键列表(转)
  12. hdu 4717 Tree2cycle(树形DP)
  13. 微信小程序基础之新建的项目文件图解
  14. TP5报错
  15. mybatis增强
  16. goldengate密码加密
  17. 使用Nginx做转发和匹配替换
  18. shell杂记
  19. 笔试题二(java面向对象、多线程、集合)
  20. HDU 1863 畅通工程 (最小生成树

热门文章

  1. WebDav协议基于HTTP 1
  2. MapReduce架构与生命周期
  3. JUC集合-BlockingQueue
  4. Codeforces Round #454
  5. node linux服务器部署 centos
  6. 关于Eclipse安装Scala插件不显示
  7. 将自己的类封装为lib的方法
  8. (转)基于MVC4+EasyUI的Web开发框架经验总结(7)--实现省份、城市、行政区三者联动
  9. macOS 不用任何第三方工具 简单两步使用 Automator 将截图转成@1x
  10. day02_20190106 基础数据类型 编码 运算符