HDU 2604 - Queuing
长度为 n 有男有女的队伍里没有 fmf 和 fff 的序列有多少
判断最后一个人无法得出结论
于是判断最后两人的递推式:
fm(n) = mm(n-1) //最后两人为fm的长度为n的队伍 只能由 mm(n-1)得到
mf(n) = fm(n-1)+ ff(n-1)
ff(n) = fm(n-1)
mm(n) = mf(n-1)+ mm(n-1)
最后S(n)=fm(n)+mf(n)+ff(n)+mm(n);
|1 1 1 1 1| | S(n-1) | | S(n) |
|0 0 0 0 1| | fm(n-1)| | fm(n)|
|0 1 0 1 0| * | mf(n-1)| = | mf(n)|
|0 1 0 0 0| | ff(n-1)| | ff(n)|
|0 0 1 0 1| | mm(n-1)| | mm(n)|
剩下的就是矩阵快速幂了。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long
struct P{
int a[][];
};
int b[]={,,,,};
int ans,n,mod;
P s,c;
P mult(P a,P b)
{
P c;
memset(c.a,,sizeof(c.a));
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
for(int k=;k<=;k++)
c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j] )%mod;
}
}
return c;
}
void ini()
{
memset(s.a,,sizeof(s.a));
memset(c.a,,sizeof(c.a));
s.a[][]=;
s.a[][]=; s.a[][]=;
s.a[][]=;
s.a[][]=; s.a[][]=;
}
void fuc(int n)
{
for(int i=;i<=;i++) c.a[i][i]=;
while(n)
{
if(n&)
{
c=mult(c,s);
}
s=mult(s,s);
n>>=;
}
}
int main()
{
while(~scanf("%d%d",&n,&mod))
{
if(n==)
{
puts("");
continue;
}
ini();
fuc(n-);
ans=;
for(int i=;i<=;i++)
{
for(int j=;j<=;j++)
{
ans=(ans+c.a[i][j]*b[j])%mod;
}
}
printf("%d\n",ans);
}
}
HDU 2604 - Queuing的更多相关文章
- HDU 2604 Queuing,矩阵高速幂
题目地址:HDU 2604 Queuing 题意: 略 分析: 易推出: f(n)=f(n-1)+f(n-3)+f(n-4) 构造一个矩阵: 然后直接上板子: /* f[i] = f[i-1] ...
- HDU 2604 Queuing 矩阵高速幂
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 2604 Queuing(矩阵高速幂)
题目地址:HDU 2604 这题仅仅要推出公式来,构造矩阵就非常easy了.问题是推不出公式来..TAT.. 从递推的思路考虑.用f(n)表示n个人满足条件的结果.假设最后一个是m则前n-1人能够随意 ...
- HDU 2604 Queuing (矩阵乘法)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!
http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE, 更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...
- hdu 2604 Queuing(矩阵快速幂乘法)
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
- HDU 2604 Queuing(矩阵快速幂)
题目链接:Queuing 题意:有一支$2^L$长度的队伍,队伍中有female和male,求$2^L$长度的队伍中除 fmf 和 fff 的队列有多少. 题解:先推导递推式:$f[i]=f[i-1] ...
- HDU 2604 Queuing(递推+矩阵)
Queuing [题目链接]Queuing [题目类型]递推+矩阵 &题解: 这题想是早就想出来了,就坑在初始化那块,只把要用的初始化了没有把其他的赋值为0,调了3,4个小时 = = 本题是可 ...
- HDU - 2604 Queuing(递推式+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...
随机推荐
- iOS 发布证书错误 Your build settings specify a provisioning profile with the UUID, no provisioning profile was found
解决办法 1.找到项目中的**.xcodeproj文件,点击右键,show package contents(打开包内容). 2.打开后找到project.pbxproj文件,用文本编辑器打开.其实就 ...
- SQL Server一些常见却不太记得住的命令
一.数据库大小查询 1. exec sp_spaceused '表名' --(SQL统计数据,大量事务操作后可能不准)2. exec sp_spaceused '表名', true ...
- java开发webservice
第一部分:相关下载配置 1.开发环境 eclipse-jee-mars-2-win32-x86_64.zip http://www.eclipse.org/downloads/index-pac ...
- SharePoint solution and feature management with PowerShell
/* Author: Jiangong SUN */ Hello, I want to introduce SharePoint solution and feature management usi ...
- html5本地存储web storage的简单使用
html5的一个非常cool的功能,就是web storage,类似于之前的cookie,不过与之不同的是,web storage 拥有本地5兆的容量可以存储,而cookie却只有4K,这是完全不能比 ...
- ubuntu 新建zend framework 项目
1. sudo apt-get install zend-framework /usr/share/php/libzend-framework-php 2. 配置include_path /etc/p ...
- python3.4+pyspider爬58同城(二)
之前使用python3.4+selenium实现了爬58同城的详细信息,这次用pyspider实现,网上搜了下,目前比较流行的爬虫框架就是pyspider和scrapy,但是scrapy不支持pyth ...
- Excel导出cs文件
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- C#基础之------控制台进程
/********************************************************************************* File:C#实现100以内两个数 ...
- HC-MAC: A Hardware-Constrained Cognitive MAC for Efficient Spectrum Management
IEEE JOURNAL ON SELECTED AREAS IN COMMUNICATIONS, VOL. 26, NO. 1, JANUARY 2008 正如上篇文章提到的,这篇论文设计的Mac协 ...