HDU 2604 Queuing 矩阵高速幂
Queuing
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2483 Accepted Submission(s): 1169
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.
3 8
4 7
4 8
6
2
1
题解
突然发现这类题目又是有一个小技巧的。先说题意,一个字符串,由f和m两种字符构成。如今的问题是,当中的子串,不出现“fff”和"fmf"的长度为L的串有多少个。
相同的,我们考虑一个充分长的串,确定他的最后两位之后,看看倒数第三位的字符是什么:
这里的x代表的是倒数第三位,能够看到,事实上这个是有规律可循的。我们仅仅要把生成fff和fmf的那种情况规避掉即可了。所以整个矩阵就是:
最后把矩阵中的所有的值所有加起来取模就可以。
代码演示样例
/****
*@author Shen
*@title HDU 2604
*/
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long int64; const int MAXN = 4;
const int MAXM = 4;
const int Mod = 1000000007; struct Matrax{
int n, m;
int64 mat[MAXN][MAXM];
Matrax(): n(-1), m(-1){}
Matrax(int _n, int _m): n(_n), m(_m){
memset(mat, 0, sizeof(mat));
}
void Unit(int _s){
n = _s; m = _s;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
mat[i][j] = (i == j)? 1: 0;
}
}
}
void print(){
printf("n = %d, m = %d\n", n, m);
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++)
printf("%16d", mat[i][j]);
printf("\n");
}
}
}; Matrax add_mod(const Matrax& a,const Matrax& b,const int64 mod){
Matrax ans(a.n, a.m);
for (int i = 0; i < a.n; i++){
for (int j = 0; j < a.m; j++){
ans.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % mod;
}
}
return ans;
} Matrax mul(const Matrax& a,const Matrax& b){
Matrax ans(a.n, b.m);
for (int i = 0; i < a.n; i++){
for (int j = 0; j < b.m; j++){
int64 tmp = 0;
for (int k = 0; k < a.m; k++){
tmp += a.mat[i][k] * b.mat[k][j];
}
ans.mat[i][j] = tmp;
}
}
return ans;
} Matrax mul_mod(const Matrax& a, const Matrax& b, const int mod){
Matrax ans(a.n, b.m);
for (int i = 0; i < a.n; i++){
for (int j = 0; j < b.m; j++){
int64 tmp = 0;
for (int k = 0; k < a.m; k++){
tmp += (a.mat[i][k] * b.mat[k][j]) % mod;
}
ans.mat[i][j] = tmp % mod;
}
}
return ans;
} Matrax pow_mod(const Matrax& a, int64 k, const int mod){
Matrax p(a.n, a.m), ans(a.n, a.m);
p = a; ans.Unit(a.n);
if (k == 0) return ans;
else if (k == 1) return a;
else {
while (k){
if (k & 1){
ans = mul_mod(ans, p, mod);
k--;
}
else {
k /= 2;
p = mul_mod(p, p, mod);
}
}
return ans;
}
} int l, m; void solve(){
if (l <= 2)
{
int root = 1;
for (int i = 0; i < l; i++)
root *= 2;
cout << root % m << endl;
return;
} Matrax ans(1, 1); //tmp = cef ^ (l - 2);
//ans = vct * tmp;
//ans = ans * beg;
//res = ans.mat[0][0] % m; Matrax cef(4, 4), tmp(4, 4);
cef.mat[0][0] = 1; cef.mat[0][3] = 1;
cef.mat[1][2] = 1;
cef.mat[2][0] = 1;
cef.mat[3][1] = 1; cef.mat[3][2] = 1;
//cef.print(); Matrax beg(4, 1), vct(1, 4);
for (int i = 0; i < 4; i++)
beg.mat[i][0] = vct.mat[0][i] = 1; tmp = pow_mod(cef, l - 2, m);
//tmp.print(); vct = mul_mod(vct, tmp, m);
ans = mul_mod(vct, beg, m);
//ans.print(); int res = ans.mat[0][0];
cout << res % m << endl;
} int main(){
while (cin >> l >> m) solve();
return 0;
}
HDU 2604 Queuing 矩阵高速幂的更多相关文章
- HDU.2640 Queuing (矩阵快速幂)
HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...
- ZOJ 3690 & HDU 3658 (矩阵高速幂+公式递推)
ZOJ 3690 题意: 有n个人和m个数和一个k,如今每一个人能够选择一个数.假设相邻的两个人选择同样的数.那么这个数要大于k 求选择方案数. 思路: 打表推了非常久的公式都没推出来什么可行解,好不 ...
- HDU 2604 Queuing,矩阵高速幂
题目地址:HDU 2604 Queuing 题意: 略 分析: 易推出: f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] ...
- hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...
- HDU 1575 Tr A(矩阵高速幂)
题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...
- HDU 2256 Problem of Precision(矩阵高速幂)
题目地址:HDU 2256 思路: (sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n; 这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6); a ...
- HDU 2254 奥运(矩阵高速幂+二分等比序列求和)
HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意: 中问题不解释. 分析: 依据floyd的算法,矩阵的k次方表示这个矩阵走了k步. 所以k ...
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
- hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)
http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p... ...
随机推荐
- Luogu P2310 【loidc,看看海】
各位大佬都用的排序和杨颙大定理,蒟蒻的我怎么也不会做(瑟瑟发抖),那么,就来一发主席树吧.我们知道线段树可以维护区间,平衡树可以维护值域那么,我们可以用线段树套平衡树来解决这个区间值域的问题线段树套平 ...
- JAVA复习笔记分布式篇:kafka
前言:第一次使用消息队列是在实在前年的时候,那时候还不了解kafka,用的是阿里的rocket_mq,当时觉得挺好用的,后来听原阿里的同事说rocket_mq是他们看来kafka的源码后自己开发了一套 ...
- C++之插入迭代器
#include<iostream> #include<vector> #include<list> #include<iterator> usingn ...
- Es官方文档整理-2.分片内部原理
Es官方文档整理-2.分片内部原理 1.集群 一个运行的Elasticsearch实例被称为一个节点,而集群是有一个或多个拥有相同claster.name配置的节点组成,他们共同承担数据和负 ...
- Failed to create the Java Virtual Machine
启动Zend Studio时出现Failed to create the Java VIrtual Machine 解决办法如下.打开安装目录下的ZendStudio.ini配置文件,作如下修改: 说 ...
- JavaScript中构造函数
构造函数:函数的另一种执行方法,执行后创建对象,并创建原型对象. 原型链:对象访问构造函数的指针. Function函数:函数对象. Object函数:所有创建对象的祖辈对象,也是由Function对 ...
- ElasticSearch实战概要
最近中美关系越来越紧张,国内经济下滑,股市一片惨淡,互联网行业越来越不景气,动不动都是跌掉几千亿市值,来写一些文档来抚慰这颗受伤的心吧... 随着互联网的发展,数据越来越重要,每个公司保存的数据也是越 ...
- [水煮 ASP.NET Web API2 方法论](1-4)从 MVC Controller 链接到 API Controller 以及反向链接
问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controller 的直接链接,或者反向链接. 解决方案 可以使用 System.Web.Htt ...
- if函数判断日期在某个时间段
SELECT ') a FROM `t_activity`;
- bzoj——2127: happiness
2127: happiness Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 2570 Solved: 1242[Submit][Status][D ...