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比较大,所以矩阵乘法快速幂优化一下递推
 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 }

 

转载于:https://www.cnblogs.com/xuwangzihao/p/4992574.html

NEU 1040 Count相关推荐

  1. 【PAT】乙级 1040 有几个PAT (25 分) c++

    1040 有几个PAT (25 分) 字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位§,第 4 位(A),第 6 位(T):第二个 PAT 是第 3 位§,第 4 位 ...

  2. PAT 1040 有几个PAT python (无超时)

    1040 有几个PAT(25)(25 分) 字符串APPAPT中包含了两个单词"PAT",其中第一个PAT是第2位( P ),第4位( A ),第6位( T ):第二个PAT是第3 ...

  3. NEU:F-大侠住店

    昨晚参加了NEU-OJ上的一个竞赛,只做出了这道题,有些NEU跟我同年级的童鞋做了三四道,小伙儿,路还很长啊--期末了,事儿也多了起来,有很多事都放不下,做题的量不多,能每天一道就不错了,加油!尽量把 ...

  4. 【bzoj 1833】【codevs 1359】 [ZJOI2010]count 数字计数(数位dp)

    1833: [ZJOI2010]count 数字计数 Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 2774  Solved: 1230 [Submit ...

  5. bzoj 1040: [ZJOI2008]骑士 树形dp

    题目链接 1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3054  Solved: 1162 [Submit] ...

  6. LeetCode刷题记录6——696. Count Binary Substrings(easy)

    LeetCode刷题记录6--696. Count Binary Substrings(easy) 目录 LeetCode刷题记录6--696. Count Binary Substrings(eas ...

  7. c++关于map的find和count的使用

    使用count,返回的是被查找元素的个数.如果有,返回1:否则,返回0.注意,map中不存在相同元素,所以返回值只能是1或0. 使用find,返回的是被查找元素的位置,没有则返回map.end(). ...

  8. 递归/归并:count of smaller numbers求逆序数

    已知数组nums,求新数组count,count[i]代表了在nums[i]右侧且比 nums[i]小的元素个数. 例如: nums = [5, 2, 6, 1], count = [2, 1, 1, ...

  9. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MB Submit: 7669  Solved: 1894 [Sub ...

最新文章

  1. Learn Jenkins the hard way (0) - Jenkins的罪与罚
  2. 2012是团购移动电商年
  3. MyBatis中使用YEAR和MONTH方法获取时间查询参数的年和月
  4. [Leedcode][JAVA][第202题][快乐数]
  5. Spark一些组件的定义
  6. CentOS7.9安装及配置
  7. CSS animation 与 transition 有何区别?
  8. 第三章 文件IO复习
  9. 一款开源Android在线音乐播放器
  10. 阶段5 3.微服务项目【学成在线】_day01 搭建环境 CMS服务端开发_15-MongoDb入门-用户...
  11. Sip 响应状态码功能对照详解
  12. Blender建模(三)
  13. Linux-基础部分
  14. 《用计算机写作文》说课稿,《我用电脑写作文》说课稿
  15. rOG魔霸新锐2022和魔霸6区别 哪个好
  16. 动态规划——爬楼梯问题(爬楼梯+最省力爬楼梯)
  17. 使用DNS来屏蔽广告
  18. Google GMS 包相关APK ANR 解决方案
  19. 使用GSAP的动画库为Bootstrap传送带制作动画
  20. 图像的腐蚀(erosion)和膨胀(dilation)

热门文章

  1. wangEditor的使用及上传图片(一)
  2. 一年后斩获腾讯T3,面试必问!
  3. TCP的三次握手、四次挥手,含泪整理面经
  4. 【Ubuntu入门到精通系列讲解】系统信息相关命令
  5. 【Applet编写应用小程序】Applet类APIの基本知识和第一个程序
  6. python【蓝桥杯vip练习题库】BASIC-25 回形取数
  7. Linux视频选用的版本,Ubuntu 及衍生版本用户安装视频播放器 SMPlayer 14.3.0
  8. 怎么升级浏览器_下载的chrome无法访问此网站怎么解决
  9. 计算机专业会比投档线高多少,比投档线高多少安全 投档线和录取线差多少
  10. object+java+equals_java-为什么SparseIntArray.equals(Object)不起作用?