• 递推+矩阵快速幂 HDU 2065


     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 //矩阵大小上限
     8 const int SIZ=100;
     9 int MOD=100;
    10 
    11 //矩阵大小为n*m,初始化全部为0
    12 struct mat
    13 {
    14     int n,m;
    15     int ar[SIZ][SIZ];
    16     mat()
    17     {
    18         memset(ar,0,sizeof(ar));
    19         n=m=SIZ;
    20     };
    21 };
    22 
    23 //矩阵乘法
    24 mat operator *(mat a,mat b)
    25 {
    26     mat c;
    27     c=mat();
    28     c.n=a.n;
    29     c.m=b.m;
    30     for(int i=1;i<=a.n;i++)
    31         for(int j=1;j<=b.n;j++)
    32             if(a.ar[i][j]!=0)
    33             for(int k=1;k<=a.m;k++)
    34             {
    35                 c.ar[i][k]+=(a.ar[i][j]*b.ar[j][k])%MOD;
    36                 c.ar[i][k]%=MOD;
    37             }
    38     return c;
    39 }
    40 
    41 //矩阵快速幂
    42 mat operator ^(mat a,long long k)
    43 {
    44     mat c;
    45     c=mat();
    46     c.n=a.n;
    47     c.m=a.m;
    48     for(int i=1;i<=a.n;i++)
    49         c.ar[i][i]=1;
    50     while(k)
    51     {
    52         if(k&1)
    53             c=c*a;
    54         a=a*a;
    55         k/=2;
    56     }
    57     return c;
    58 }
    59 
    60 int main()
    61 {
    62     int T;
    63     while(scanf("%d",&T)!=EOF)
    64     {
    65         if(T==0)
    66             break;
    67         for(int l=1;l<=T;l++)
    68         {
    69             long long n;
    70             mat a;
    71             a.m=a.n=4;
    72             for(int i=1;i<=4;i++)
    73             {
    74                 for(int j=1;j<=4;j++)
    75                 {
    76                     a.ar[i][j]=1;
    77                     if(i==j)
    78                         a.ar[i][j]=2;
    79                     if(i+j==5)
    80                         a.ar[i][j]=0;
    81                 }
    82             }
    83             scanf("%I64d",&n);
    84             mat b=a^n;
    85             int ans=b.ar[1][1];
    86             cout<<"Case "<<l<<": "<<ans<<endl;
    87         }
    88         cout<<endl;
    89     }
    90     return 0;
    91 }
    View Code
  • 相关阅读:
    信息检索笔记
    北大课程(变态心理学)
    My life
    Excel小技巧(随机点名)
    Flask基础
    CTF
    GDB
    LD_PRELOAD
    AFL-数据变异
    AFL入门
  • 原文地址:https://www.cnblogs.com/wsruning/p/4768716.html
Copyright © 2020-2023  润新知