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