SDAU 课程练习3 1009

Problem I

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 6   Accepted Submission(s) : 1
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. <br><center><img src=../../../data/images/C154-1005-1.jpg> </center><br>&nbsp;&nbsp;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 2<sup>L</sup> 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.<br>Your task is to calculate the number of E-queues mod M with length L by writing a program.<br>
 

Input
Input a length L (0 <= L <= 10 <sup>6</sup>) 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
 
题目大意:

求出所有的字符全排列,排除 含有 fff  和 fmf  的,用剩下的个数  模  m  就是最后的结果。

思路:

关于递推公式是怎么得到的,希望同学们能想一下。
f (n) = f(n-1) + f (n-3)+ f (n-4)
其实就是如果最后一个字母是 m  那么前面的所有都满足喽。
如果是  f   那么再往前考虑,mmf  fmf  mff  fff   只有两个满足条件。如果是  mmf  的话,都成立,所以加上  f (n-3) 如果是  mff  满足的只有  mmff  所以加上 f (n-4)
然后那?如果用循环配合数组进行打表的话!!那是时间会爆掉的。因为他是   10 ^6  所以你的复杂度不能是  n^2    
如果用     for (i=0;i< 10^6; i++)
f (n) = f(n-1) + f (n-3)+ f (n-4)
显然不仅超时还会溢出的对吗?
矩阵连续次幂可以快速处理递推,这个是个新知识。

感想:

今天搞了一个斐波那契的递推,但是wa     问了鹏哥哥,他说数太大,不可以用矩阵处理。

AC代码:

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
int N,m;
struct matrix
{
       int a[4][4];
}origin,res;
matrix multiply(matrix x,matrix y)
{
       matrix temp;
       memset(temp.a,0,sizeof(temp.a));
       for(int i=0;i<4;i++)
       {
               for(int j=0;j<4;j++)
               {
                       for(int k=0;k<4;k++)
                       {
                               temp.a[i][j]+=(x.a[i][k]*y.a[k][j])%m;
                               temp.a[i][j]%=m;
                       }
               }
       }
       return temp;
}
void calc(int n)
{
     while(n)
     {
             if(n&1)
                    res=multiply(res,origin);

             origin=multiply(origin,origin);
             n>>=1;
     }
}
int main()
{
    int ff[5]={9,6,4,2};
    int i,j;
    matrix aaa;
    //freopen("r.txt","r",stdin);
    while(~scanf("%d%d",&N,&m))
    {
        if(N==0)
        {
            cout<<0<<endl;
            continue;
        }
        if(N<=4)
        {
            cout<<ff[4-N]%m<<endl;
            continue;
        }
        int doudou[5]={0};
        memset(origin.a,0,sizeof(origin.a));
        memset(aaa.a,0,sizeof(aaa.a));
        aaa.a[0][0]=9;
        aaa.a[1][0]=6;
        aaa.a[2][0]=4;
        aaa.a[3][0]=2;
        origin.a[0][0]=origin.a[0][2]=origin.a[0][3]=origin.a[1][0]=origin.a[2][1]=origin.a[3][2]=1;
        res.a[0][0]=res.a[1][1]=res.a[2][2]=res.a[3][3]=1;
        calc(N-4);
        res=multiply(res,aaa);
        cout<<res.a[0][0]%m<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值