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("%d\n",0);
63       else if(len<=4)printf("%d\n",f[len-1]%m);
64       else{
65       init();
66      pow(len-4);
67      printf("%d\n",(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 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏禁心尽力

关于web.xml3.0启动报错

九月 08, 2017 10:18:19 上午 org.apache.tomcat.util.digester.SetPropertiesRule begin ...

34770
来自专栏ml

HDUOJ-----1085Holding Bin-Laden Captive!

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limi...

283110
来自专栏大数据学习笔记

Hadoop基础教程-第9章 HA高可用(9.3 HDFS 高可用运行)(草稿)

第9章 HA高可用 9.3 HDFS 高可用运行 9.3.1 HA节点规划 节点 IP Zookeeper NameNode JournalNode Da...

28650
来自专栏ml

HDUOJ---(4708)Herding

Herding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J...

357100
来自专栏CreateAMind

代码:Learning by Playing –Solving Sparse Reward Tasks from Scratch

19310
来自专栏ml

HDUOJ---hello Kiki

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K...

28490
来自专栏ml

hdu 1695 GCD(莫比乌斯反演)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/...

41260
来自专栏Linyb极客之路

你该怎么做?当你发现springboot扫描controller没有生效

加上@SpringBootApplication为啥就能扫描到,其实我也不知道。那就看下这个注解到底是个啥样的奇葩,它的注解构造了解一下

3.9K30
来自专栏算法修养

POJ-1322 Chocolate(概率DP)

Chocolate Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 92...

33930
来自专栏ml

HDU----(4291)A Short problem(快速矩阵幂)

A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32...

35260

扫码关注云+社区

领取腾讯云代金券