Problem I
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.
L (0 <= L <= 10 6) and
M.
M(1 <= M <= 30) where K is the number of E-queues with length
L.
#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的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- 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 ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [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 ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
随机推荐
- day18<集合框架+>
集合框架(Map集合概述和特点) 集合框架(Map集合的功能概述) 集合框架(Map集合的遍历之键找值) 集合框架(Map集合的遍历之键值对对象找键和值) 集合框架(Map集合的遍历之键值对对象找键和 ...
- Centos6.7安装chrome
cd /etc/yum.repos.dwget http://people.centos.org/hughesjr/chromium/6/chromium-el6.repo yum install c ...
- EF 6.0
最近又开始研究EF框架了 哎 搞的东西太杂了 网上的参考了一篇博客 但是他是基于EF 4.0之前做的 所以自己基于他的博客来构造EF 6.0的使用基础 命名空间不同: 旧版本:using System ...
- bzoj1027 [HNOI2004]打鼹鼠
[HNOI2004]打鼹鼠 2014年5月2日2,8605 Description 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿Q编写了一个打鼹鼠 ...
- Tomcat 设置自启动时遇到的错误问题与解决方案
首先,今天在做tomcat开机自启动时,原本很简单的一个问题,但却浪费了很长时间: 首先系统环境采用的是Window10,设置Tomcat自启动过程当中需要注意的是:JDK的版本和Tomcat的位数必 ...
- [js高手之路] html5 canvas动画教程 - 匀速运动
匀速运动:指的是物体在一条直线上运动,并且物体在任何相等时间间隔内通过的位移都是相等的.其实就是匀速直线运动,它的特点是加速度为0,从定义可知,在任何相等的时间间隔内,速度大小和方向是相同的. < ...
- sql server 2000的安装
一.安装sql 二.启动sql 三.查看sql版本 RTM版本,需要打补丁 四.安装SP4
- springboot使用zookeeper(curator)实现注册发现与负载均衡
最简单的实现服务高可用的方法就是集群化,也就是分布式部署,但是分布式部署会带来一些问题.比如: 1.各个实例之间的协同(锁) 2.负载均衡 3.热删除 这里通过一个简单的实例来说明如何解决注册发现和负 ...
- [js高手之路]匀速运动与实例实战(侧边栏,淡入淡出)
javascript中,如何让一个元素(比如div)运动起来呢? 设置基本的样式,一定要让div有定位( 当然用margin的变化也可以让元素产生运动效果 ); <style> div { ...
- OpenGL ES2.0贴图
1.定义传入着色器的顶点数据及索引 //传入结构体 typedef struct { ]; ]; } Vertex; //顶点数据 const Vertex Vertices[] = { {{, -, ...