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 ;
}
 

NEU 1040 Count的更多相关文章

  1. nodejs api 中文文档

    文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...

  2. &lt;LeetCode OJ&gt; 204. Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 分析: 思路首先:一个数不是合数就 ...

  3. 1040 有几个PAT (25 分)

    题目链接:1040 有几个PAT (25 分) 做这道题目,遇到了新的困难.解决之后有了新的收获,甚是欣喜! 刚开始我用三个vector数组存储P A T三个字符出现的位置,然后三层for循环,根据字 ...

  4. 【九度OJ】题目1040:Prime Number 解题报告

    [九度OJ]题目1040:Prime Number 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1040 题目描述: Ou ...

  5. C#中Length和Count的区别(个人观点)

    这篇文章将会很短...短到比你的JJ还短,当然开玩笑了.网上有说过Length和count的区别,都是很含糊的,我没有发现有 文章说得比较透彻的,所以,虽然这篇文章很短,我还是希望能留在首页,听听大家 ...

  6. [PHP源码阅读]count函数

    在PHP编程中,在遍历数组的时候经常需要先计算数组的长度作为循环结束的判断条件,而在PHP里面对数组的操作是很频繁的,因此count也算是一个常用函数,下面研究一下count函数的具体实现. 我在gi ...

  7. EntityFramework.Extended 实现 update count+=1

    在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...

  8. 学习笔记 MYSQL报错注入(count()、rand()、group by)

    首先看下常见的攻击载荷,如下: select count(*),(floor(rand(0)*2))x from table group by x; 然后对于攻击载荷进行解释, floor(rand( ...

  9. count(*) 与count (字段名)的区别

    count(*) 查出来的是:结果集的总条数 count(字段名) 查出来的是: 结果集中'字段名'不为空的记录的总条数

随机推荐

  1. (转载) popupWindow 指定位置上的显示

    popupWindow 指定位置上的显示 标签: androidpopupWindowpopupWindow具体位置放置 2014-07-09 16:23 1114人阅读 评论(0) 收藏 举报  分 ...

  2. PostgreSQL导出表中数据

    下边的步骤详细讲述了从Postgres数据库中导出数据的方法: (1)将PostgreSQL数据库的psql工具所在的路径添加到系统的环境变量中:(2)运行cmd,在窗口中输入psql,会有提示输入口 ...

  3. 还是UVa340

    #include<stdio.h> #define maxn 1010 int main() { int num,a[maxn],i,j,b[maxn]; ; &&num) ...

  4. (转)RabbitMQ学习之集群部署

    http://blog.csdn.net/zhu_tianwei/article/details/40931971 我们先搭建一个普通集群模式,在这个模式基础上再配置镜像模式实现高可用,Rabbit集 ...

  5. Flex元素布局规则总结,以及布局和容器

    一.Flex中的元素分类从功能层面可以把Flex中的元素分为组件(Components)和容器(Containers)两大类:组件 - 是指那类具有明确交互或数据展示功能的元素,例如Button.Ch ...

  6. vc++创建Win32 Application窗体过程

    #include<windows.h>#include<stdio.h>LRESULT CALLBACK WinSunProc( HWND hwnd, UINT uMsg, W ...

  7. 探索JS引擎工作原理 (转)

    这篇文章从相对底层的角度介绍了js引擎的工作 引入了 静态作用域 执行环境上下文(context) 等概念 , http://www.cnblogs.com/onepixel/p/5090799.ht ...

  8. spring boot --部署war到tomcat中

    1.在pom文件中,将默认的jar包打包,改成以war包打包:<!--打包方式--><packaging>war</packaging> 2.将项目在doc命令中执 ...

  9. 前端异步编程之Promise和async的用法

    传统的异步解决方案采用回调函数和事件监听的方式,而这里主要记录两种异步编程的新方案: ES6的新语法Promise ES2017引入的async函数 Generator函数(略) Promise的含义 ...

  10. WEBGL学习【四】模型视图矩阵

    <html lang="zh-CN"> <!--服务器运行地址:http://127.0.0.1:8080/webgl/LearnNeHeWebGL/NeHeWe ...