Problem Description
Queues and
Priority Queues are data structures which are known to most
computer scientists. The Queue occurs often in our daily life.
There are many people lined up at the lunch time.

I" title="Problem I">


  Now we define that ‘f’ is short
for female and ‘m’ is short for male. If the queue’s length is L,
then there are 2L numbers of
queues. For example, if L = 2, then they are ff, mm, fm, mf . If
there exists a subqueue as fmf or fff, we call it O-queue else it
is a E-queue.

Your task is to calculate the number of E-queues mod M with length
L by writing a program.
Input
Input a length
L (0 <= L <= 10 6) and
M.
Output
Output K mod
M(1 <= M <= 30) where K is the number of E-queues with length
L.
Sample Input
3 8
4 7
4 8
Sample Output
6
2
1
题意:n个人排队,f表示女,m表示男,包含子串fmf和fff的序列为O队列,否则为E队列,有多少个序列为E队列。
解题思路:刚开始想的简单啊,直接递推公式,但是现实太残酷。用普通方法会超时的,这个星期就想这一个题去了,看了巨巨的博客,才知道还有矩阵这个好东西;具体矩阵怎么用,见我总结的《关于矩阵相乘快速幂的理解及其用处
感悟:这星期真是堕落。。。。。才做了这么一个题;
代码:
#include

#include

#include

using namespace std;

int n,mod;

struct Matrix{

    int
arr[4][4];

};

Matrix unit,init;

//矩阵相乘的函数

Matrix Mul(Matrix a,Matrix b){

    Matrix
c;

    for(int
i=0;i<4;i++)

       
for(int j=0;j<4;j++){

           
c.arr[i][j]=0;

           
for(int k=0;k<4;k++)

               
c.arr[i][j]=(c.arr[i][j]+a.arr[i][k]*b.arr[k][j]%mod)%mod;

           
//cout<<c.arr[i][j]<<endl;

           
c.arr[i][j]%=mod;

       
}

    return
c;

}

//进行F[n]=F[n-1]+F[n-3]+F[n-4]

Matrix Pow(Matrix a,Matrix b,int k){

   
while(k){

       
if(k&1){

           
b=Mul(b,a);

       
}

       
a=Mul(a,a);

       
//cout<<k<<endl;

       
k>>=1;//k/=2但是前者快点

    }

    return
b;

}

//初始化

void Init(){

    for(int
i=0;i<4;i++)

       
for(int j=0;j<4;j++){

           
init.arr[i][j]=0;

           
unit.arr[i][j]=0;

       
}

   
//递推的前四项

   
unit.arr[0][0]=9,  
unit.arr[0][1]=6,  
unit.arr[0][2]=4,  
unit.arr[0][3]=2;

   
//设置递推关系的矩阵

   
init.arr[0][0]=1,  
init.arr[0][1]=1,  
init.arr[1][2]=1,  
init.arr[2][0]=1,

   
init.arr[2][3]=1,  
init.arr[3][0]=1;

}

int main(){

   
//freopen("in.txt","r",stdin);

   
Init();

   
while(~scanf("%d%d",&n,&mod)){

       
if(n<=4){

           
if(n==0)

               
printf("0");

           
else if(n==1)

               
printf("%d\n",2%mod);

           
else if(n==2)

               
printf("%d\n",4%mod);

           
else if(n==3)

               
printf("%d\n",6%mod);

           
else if(n==4)

               
printf("%d\n",9%mod);

           
continue;

       
}

       
Matrix res=Pow(init,unit,n-4);

       
printf("%d\n",res.arr[0][0]%mod);

    }

    return
0;

}


Problem I的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  10. PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案

    $s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...

随机推荐

  1. grunt学习笔记1 理论知识

    你需要检查js语法错误,然后再去压缩js代码.如果这两步你都去手动操作,会耗费很多成本.Grunt就能让你省去这些手动操作的成本. “—save-dev”的意思是,在当前目录安装grunt的同时,顺便 ...

  2. Spring Boot Document Part II(上)

    Part II. Getting started 这一章内容适合刚接触Spring Boot或者"Spring"家族的初学者!随着安装指导说明,你会发现对Spring boot有一 ...

  3. load data(sql)

    一般对于数据库表的插入操作,我们都会写程序执行插入sql,插入的数据少还可以,如果数据多了.执行效率上可能就不太理想了.load data语句用于高速地从一个文本文件中读取数据,装载到一个表中,相比于 ...

  4. 使用LayUI展示数据

    LayUI是一款免费,开源,轻量级的前端cms框架,适用于企业后端,能快速上手开发,集成了常用的组件,还有完善的文档和社区. 点击查看 文档地址 下载框架 使用: 1.把这个5个文件项都拷贝到项目中 ...

  5. Clojure——学习迷宫生成

    背景 初学clojure,想着看一些算法来熟悉clojure语法及相关算法实现. 找到一个各种语言生成迷宫的网站:http://rosettacode.org/wiki/Maze_generation ...

  6. bzoj4236 JOIOJI hash 模拟

    JOIOJI桑是JOI君的叔叔."JOIOJI"这个名字是由"J.O.I"三个字母各两个构成的. 最近,JOIOJI桑有了一个孩子.JOIOJI桑想让自己孩子的 ...

  7. Java历程-初学篇 Day09 冒泡排序

    冒泡排序 冒泡排序(Bubble Sort)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是 ...

  8. 小程序解释HTML富文本的两种办法

    今天写着着代码,读取数据库的内容时突然跳出"<span>.<p>. "这些HTML标签.字符,吓一跳:本来如果是写HTML.JS倒也没什么,但是我在写小程序 ...

  9. 关于安全性问题:(XSS,csrf,cors,jsonp,同源策略)

    关于安全性问题:(XSS,csrf,cors,jsonp,同源策略) Ajax 是无需刷新页面就能从服务器获取数据的一种方法.它的核心对象是XHR,同源策略是ajax的一种约束,它为通信设置了相同的协 ...

  10. thrift例子:python客户端/java服务端

    java服务端的代码请看上文. 1.说明: 这两篇文章其实解决的问题是,当使用python去访问大数据线上集群的时候,遇到两个问题: 1)python-hadoop和python-hive相关包链接不 ...