hdu---(2604)Queuing(矩阵快速幂)
Queuing
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2796 Accepted Submission(s): 1282
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.
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
Author
WhereIsHeroFrom
首先我们不考虑去模的问题:
l = 0 0 种
l = 1 e的数目有 f,m: 2 种
l = 2 ...........ff,mm,fm,mf 4种
l = 3 6
l = 4 9
l = 5 15
l = 6 25
f5=f4+f3+f1;
f6=f5+f4+f2;
-------> fn={ fn-1+fn-3+fn-4 n>4;
由齐次方程构造矩阵.....
|fn | |1,0,1,1| |fn-1|
|fn-1| |1,0,0,0| |fn-2|
|fn-2| = |0,1,0,0|*|fn-3|
|fn-3| |0,0,1,0| |fn-4|
代码:
1 //#define LOCAL
2 #include<cstdio>
3 #include<cstring>
4 using namespace std;
5 //matrix --> ¾ØÕó
6 int mat[4][4];
7 int ans[4][4];
8 int len,m;
9
10 void init()
11 {
12 int cc[4][4]={
13 {1,0,1,1},{1,0,0,0},
14 {0,1,0,0},{0,0,1,0}};
15
16 for(int i=0;i<4;i++)
17 {
18 for(int j=0;j<4;j++)
19 {
20 mat[i][j]=cc[i][j];
21 if(i==j) ans[i][j]=1;
22 else ans[i][j]=0;
23 }
24 }
25 }
26 void Matrix(int a[][4],int b[][4]) //¾ØÕóÏà³Ë
27 {
28 int i,j,k;
29 int c[4][4]={0};
30 for(j=0;j<4;j++){
31 for(i=0;i<4;i++){
32 for(k=0;k<4;k++){
33 c[j][i]=(c[j][i]+a[j][k]*b[k][i])%m;
34 }
35 }
36 }
37
38 for(j=0;j<4;j++)
39 for(i=0;i<4;i++)
40 a[j][i]=c[j][i];
41
42 }
43
44 void pow(int n)
45 {
46 while(n>0)
47 {
48 if(n&1) Matrix(ans,mat);
49 n>>=1;
50 if(n==0) break;
51 Matrix(mat,mat);
52 }
53 }
54 int main()
55 {
56 #ifdef LOCAL
57 freopen("test.in","r",stdin);
58 #endif
59 int f[4]={2,4,6,9};
60 while(scanf("%d%d",&len,&m)!=EOF)
61 {
62 if(len==0)printf("%dn",0);
63 else if(len<=4)printf("%dn",f[len-1]%m);
64 else{
65 init();
66 pow(len-4);
67 printf("%dn",(ans[0][0]*f[3]+ans[0][1]*f[2]+ans[0][2]*f[1]+ans[0][3]*f[0])%m);
68 }
69 }
70 return 0;
71 }
- 厚土Go学习笔记 | 38. goroutine轻量级线程
- 厚土Go学习笔记 | 36. web服务指定路径下的get参数接收与处理
- Java开发Spring笔记第二天
- PHP调用Go服务的正确方式 - Unix Domain Sockets
- 一条看似平常的报警邮件所做的分析(r8笔记第9天)
- 55. 上传文件(Web版) | 厚土Go学习笔记
- R语言与机器学习学习笔记(分类算法
- 54. 心跳的实现 | 厚土Go学习笔记
- 通过错误的sql来测试推理sql的解析过程(二) (r8笔记第7天)
- 53. Socket服务三次握手的示例 | 厚土Go学习笔记
- R分词继续,"不|知道|你在|说|什么"分词添加新词
- Java开发Spring第一天
- 关于R安装中文分词包安装不上的问题install.packages("tm")
- dataguard备库的数据文件的迁移实战(r8笔记第24天)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- MySQL复杂where条件分析
- 超链接标签
- 【剑指Offer】二叉树的镜像
- 【redis6.0.6】redis源码慢慢学,慢慢看 -- 第四天:提纲掣领main函数(server)
- redis学习(六)
- 原生JS实现一个Ajax跨域请求
- 深入理解类加载机制:拨开迷雾见真章
- 修复postgres安装错误 Problem running post-install step. Installation may not complete correctly The datab
- automagica 调用windows画图以及登录qq
- 数据库PostrageSQL-高级特性
- 数据库PostgreSQL-安装
- CentOS 7 安装 PHP 7.4.0 正式版
- ABAP实现设计模式里的观察者-发布者模式
- 如何在SAP S/4HANA里创建自定义的partner function
- Rust 往事 | Loop 和 While True 之争