HDU-2604 Queuing(矩阵快速幂)

Queuing

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6076    Accepted Submission(s): 2643

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

Source

HDU 1st “Vegetable-Birds Cup” Programming Open Contest

Recommend

lcy

题目大意:给定长度L,由m、f组成的队列,如果是fmf、fff则是E队列,问长为L的队列中最多有多少E队列(mod K)

解题思路:前几个例子不难发现F5 = F1+F3+F4。所以可以得出如下关系:

1  0  1  1        F1            F5

1  0  0  0        F2            F1

*              =

0  1  0  0        F3            F2

0  0  1  0        F4       F3

所以就是计算初始矩阵a的l次幂最后mod k即可

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,mod;
struct matrix
{
    long long m[4][4];
    matrix operator*(const matrix& a)const
    {
        matrix temp;
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                temp.m[i][j] = 0;
                for(int k=0;k<4;k++)
                {
                    temp.m[i][j] += m[i][k]*a.m[k][j]%mod;
                    temp.m[i][j] %= mod;
                }
            }
        }
        return temp;
    }
};

int ks(matrix &a)
{
    if(n<=3)
        return (2*n)%mod;
    if(n==4)
        return 9%mod;
    n -= 4;
    matrix ans;
    memset(ans.m,0,sizeof(ans));
    for(int i=0;i<4;i++)
    {
        ans.m[i][i] = 1;
    }
    while(n)
    {
        if(n%2)
            ans = ans*a;
        a = a*a;
        n /= 2;
    }

    int sum=0;
    sum+=ans.m[0][0]*9%mod;
    sum+=ans.m[0][1]*6%mod;
    sum+=ans.m[0][2]*4%mod;
    sum+=ans.m[0][3]*2%mod;
    sum %= mod;
    return sum;
}
int main()
{
    matrix a;
    while(scanf("%d %d",&n,&mod)!=EOF)
    {
        memset(a.m,0,sizeof(a.m));
        a.m[0][0] = a.m[0][2] = a.m[0][3] = 1;
        a.m[1][0] = a.m[2][1] = a.m[3][2] = 1;
        printf("%d\n",ks(a));
    }
}
时间: 2024-10-10 18:44:16

HDU-2604 Queuing(矩阵快速幂)的相关文章

HDU - 2604 Queuing(矩阵快速幂或直接递推)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 题意:给出字符串长度L,并且字符串只由'f','m'构成,有2^L种情况,问在其中不包含'fmf','fff'的字符串有多少个. 1.直接递推,虽然过了,但是数据稍微大点就很可能TLE,因为代码上交上去耗时还是比较长的(感觉数据有点水)╭(′▽`)╭(′▽`)╯( 1 #include <iostream> 2 #include <cstdio> 3 #include <c

HDU 2604 Queuing 矩阵高速幂

QueuingTime Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2483    Accepted Submission(s): 1169 Problem Description Queues and Priority Queues are data structures which are known to most computer s

Hdu 4965(矩阵快速幂)

题目链接 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 87    Accepted Submission(s): 39 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a

ZOJ 3690 &amp; HDU 3658 (矩阵快速幂+公式递推)

ZOJ 3690 题意: 有n个人和m个数和一个k,现在每个人可以选择一个数,如果相邻的两个人选择相同的数,那么这个数要大于k 求选择方案数. 思路: 打表推了很久的公式都没推出来什么可行解,好不容易有了想法结果WA到天荒地老也无法AC.. 于是学习了下正规的做法,恍然大悟. 这道题应该用递推 + 矩阵快速幂. 我们设F(n) = 有n个人,第n个人选择的数大于k的方案数: G(n) = 有n个人,第n个人选择的数小于等于k的方案数: 那么递推关系式即是: F(1)=m?k,G(1)=k F(n

HDU 1575 &amp;&amp; 1757 矩阵快速幂&amp;&amp;构造矩阵入门

HDU 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2912    Accepted Submission(s): 2167 Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据.每组

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

HDU 1575-Tr A(矩阵快速幂)

Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3169    Accepted Submission(s): 2367 Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有

2017 ACM/ICPC Asia Regional Shenyang Online:number number number hdu 6198【矩阵快速幂】

Problem Description We define a sequence F: ? F0=0,F1=1;? Fn=Fn?1+Fn?2 (n≥2). Give you an integer k, if a positive number n can be expressed byn=Fa1+Fa2+...+Fak where 0≤a1≤a2≤?≤ak, this positive number is mjf?good. Otherwise, this positive number is 

Reading comprehension HDU - 4990 (矩阵快速幂 or 快速幂+等比数列)

for(i=1;i<=n;i++) { if(i&1)ans=(ans*2+1)%m; else ans=ans*2%m; } 给定n,m.让你用O(log(n))以下时间算出ans. 打表,推出 ans[i] = 2^(i-1) + f[i-2] 故 i奇数:ans[i] = 2^(i-1) + 2^(i-3) ... + 1; i偶数:ans[i] = 2^(i-1) + 2^(i-3) ... + 2; 故可以用等比数列求和公式. 公式涉及除法.我也没弄懂为啥不能用逆元,貌似说是啥逆元

hdu 5667 Sequence 矩阵快速幂

题目链接:hdu 5667 Sequence 思路:因为fn均为a的幂,所以: 这样我们就可以利用快速幂来计算了 注意: 矩阵要定义为long long,不仅仅因为会爆,还会无限超时 要对a%p==0特判,以为可能出现指数%(p-1)==0的情况,那么在快速幂的时候返回的结果就是1而不是0了 /************************************************************** Problem:hdu 5667 User: youmi Language: