首页 > 其他分享 >hdu:Queuing(矩阵快速幂)

hdu:Queuing(矩阵快速幂)

时间:2023-01-14 17:00:17浏览次数:35  
标签:hdu matrix int 矩阵 long Queuing queue length mod

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.


输入样例

3 8
4 7
4 8

输出样例

6
2
1
注意首项的选择,以及ans的赋值
附ac代码
#include<bits/stdc++.h>
using namespace std;
const int N=4;
struct matrix{
  long long ma[N][N];
};
int l,m;
matrix muti(matrix a,matrix b)
{
    matrix c;
    for(int i=0;i<4;++i)
      for(int j=0;j<4;++j)
      c.ma[i][j]=0;
    for(int i=0;i<4;++i)
      for(int j=0;j<4;++j)
       for(int z=0;z<4;++z)
       c.ma[i][j]+=(a.ma[i][z]%m*(b.ma[z][j]%m)%m);
    return c;
}
matrix pow_ma(matrix a,int k)
{
    if(k==1) return a;
    matrix s;
    s=pow_ma(muti(a,a),k/2);
    if(k%2) s=muti(s,a);
    return s;
}
int main()
{
    int f[5];
    f[1]=2,f[2]=4,f[3]=6,f[4]=9;
    //f[0]不为1,为0,故f[4]需要单独讨论
    while(scanf("%d%d",&l,&m)==2)
    {
        if(l<=4)
        {printf("%d\n",f[l]%m);continue;
        }
        matrix ans;
        for(int i=0;i<4;++i)
          for(int j=0;j<4;++j)
           ans.ma[i][j]=0;
        ans.ma[0][0]=1,ans.ma[0][2]=1,ans.ma[0][3]=1;
        ans.ma[1][0]=1,ans.ma[2][1]=1,ans.ma[3][2]=1;
        ans=pow_ma(ans,l-4);
//注意pow_ma时void函数需要再对ans赋值 long long sum=0; for(int i=0;i<4;++i) sum+=(ans.ma[0][i]%m*(f[4-i]%m))%m; printf("%lld\n",sum%m); } return 0; }

 

标签:hdu,matrix,int,矩阵,long,Queuing,queue,length,mod
From: https://www.cnblogs.com/ruoye123456/p/17052069.html

相关文章

  • 杨氏矩阵
    问题:有一个数字矩阵,矩阵的每行从左到右是递增的,矩阵从上到下是递增的,请编写程序在这样的矩阵中查找某个数字是否存在。要求:时间复杂度小于O(N);杨氏矩阵普及:杨氏矩阵是对组......
  • hdu:Problem Description Lele now is thinking about a s(矩阵快速幂)
    ProblemDescriptionLelenowisthinkingaboutasimplefunctionf(x).Ifx<10f(x)=x.Ifx>=10f(x)=a0 f(x-1)+a1 f(x-2)+a2 f(x-3)+……+a9 ......
  • 代码随想录算法训练营第二天|977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II
    一、参考资料有序数组的平方题目链接:https://leetcode.cn/problems/squares-of-a-sorted-array/文章讲解:https://programmercarl.com/0977.有序数组的平方.html视频讲......
  • 【Pytorch】多维矩阵的加法
    目录​​简介​​​​问题描述​​​​测试​​​​解释​​​​结语​​简介Hello!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~ ଘ(੭ˊᵕˋ)੭昵称:海轰......
  • 【Pytorch】将矩阵中的元素按照区间重新赋值
    目录​​简介​​​​场景描述​​​​解决方法​​​​结语​​简介Hello!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~ ଘ(੭ˊᵕˋ)੭昵称:海轰标签:程序......
  • 【Pytorch】计算矩阵中向量之间的两两相似性
    目录​​简介​​​​场景描述​​​​解决方法​​​​结语​​简介Hello!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~ ଘ(੭ˊᵕˋ)੭昵称:海轰标签:程序......
  • P1005 [NOIP2007 提高组] 矩阵取数游戏
    题目传送门前言今天依旧是不写高精的一天呢!(是的,这位作者又只拿了开\(LL\)的\(\color{yellow}{60}\)分)思路描述看到数据\(n,m\le80(30)\)就知道数组可以任性开,......
  • HDU 5306 Gorgeous Sequence
    \(HDU\)\(5306\)\(Gorgeous\)\(Sequence\)标签:区间最值操作,吉司机线段树,简单模板题一、题目描述现在有这样的一个问题:你有一个长度为\(n\)(\(n≤1e6\))的序列,你......
  • hdu:Ignatius and the Princess II(全排列,dfs)
    ProblemDescriptionNowourherofindsthedoortotheBEelzebubfeng5166.Heopensthedoorandfindsfeng5166isabouttokillourprettyPrincess.Butnow......
  • 74. 搜索二维矩阵
    问题链接https://leetcode.cn/problems/search-a-2d-matrix/description/解题思路我们可以确定,数据是有序的。所以我们有2种办法用二分来解决。第一种,我们可以写个下标......