zoukankan      html  css  js  c++  java
  • hdu 1757 A Simple Math Problem(矩阵快速幂乘法)

    Problem Description
    Lele now is thinking about a simple function f(x).
    
    If x < 10 f(x) = x.
    If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
    And ai(0<=i<=9) can only be 0 or 1 .
    
    Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
    Input
    The problem contains mutiple test cases.Please process to the end of file.
    In each case, there will be two lines.
    In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
    In the second line , there are ten integers represent a0 ~ a9.
    Output
    For each case, output f(k) % m in one line.
     
    Sample Input
    10 9999
    1 1 1 1 1 1 1 1 1 1
    20 500
    1 0 1 0 1 0 1 0 1 0
     
    Sample Output
    45 
    104
     

    把问题转化为求矩阵的n-9次幂就行了;

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 int k,MOD;
     6 int a[10];
     7 int f[10];
     8 struct Matrix
     9 {
    10     int m[10][10];
    11 }matrix;
    12 
    13 Matrix Mul(Matrix a,Matrix b)
    14 {
    15     Matrix res;
    16     int i,j,k;
    17     for(i=0;i<10;i++)
    18     {
    19         for(j=0;j<10;j++)
    20         {
    21             res.m[i][j] = 0;
    22             for(k=0;k<10;k++)
    23                 res.m[i][j] = (res.m[i][j]+(a.m[i][k]*b.m[k][j]))%MOD;
    24         }
    25     }
    26     return res;
    27 }
    28 
    29 Matrix fastm(Matrix a,int b)
    30 {
    31     Matrix res;
    32     memset(res.m,0,sizeof(res.m));
    33         for(int i=0;i<10;i++)
    34             res.m[i][i] = 1;
    35     while(b)
    36     {
    37         if(b&1)
    38             res = Mul(res,a);
    39         a = Mul(a,a);
    40         b >>= 1;
    41     }
    42     return res;
    43 }
    44 void init()
    45 {
    46     for(int i=0;i<=9;i++)
    47     {
    48         f[i]=i;
    49     }
    50 }
    51 int main()
    52 {
    53     init();
    54     while(scanf("%d%d",&k,&MOD)==2)
    55     {
    56         for(int i=0;i<=9;i++)
    57         {
    58             scanf("%d",&a[i]);
    59         }
    60         if(k<10)
    61         {
    62             printf("%d
    ",k%MOD);
    63             continue;
    64         }
    65         
    66         memset(matrix.m,0,sizeof(matrix.m));
    67         for(int i=0;i<=9;i++)
    68           matrix.m[0][i]=a[i];
    69         for(int i=1;i<=9;i++)
    70             matrix.m[i][i-1] = 1;
    71         Matrix ans=fastm(matrix,k-9);
    72         Matrix cnt;
    73         for(int i=0;i<10;i++)
    74         {
    75             cnt.m[i][0]=f[9-i];
    76         }
    77         Matrix p=Mul(ans,cnt);
    78         printf("%d
    ",p.m[0][0]%MOD);
    79     }
    80     return 0;
    81 }
    View Code
  • 相关阅读:
    php命令注入
    mysql事物
    安装php环境
    移除服务器缓存实例
    show user profile synchronization tools
    manual start user profile import
    JSON is undefined. Infopath Form People Picker in SharePoint 2013
    asp.net web 应用站点支持域账户登录
    Load sharepoint envirement by powershell
    sharepoint 2016 download
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5041449.html
Copyright © 2011-2022 走看看