• 【矩阵快速幂】【杭电OJ1757】


    http://acm.hdu.edu.cn/showproblem.php?pid=1757

    A Simple Math Problem

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 5829    Accepted Submission(s): 3555

    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

     题目分析:看到 1)f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);【一个递推式】

            2) k<2*10^9 , m < 10^5【数据超大】

          就知道是矩阵快速幂了【好吧..我之前并不知道..qaq..】

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 using namespace std;
     6 long long n,k;
     7 void mul(long long wqw[10],long long tat[10][10])
     8 {
     9     long long or2[10];
    10 //    cout << 
    11     memset(or2,0,sizeof(or2));
    12     for(int i = 0 ; i < 10  ; i++)
    13     {
    14         for(int j = 0 ; j < 10 ; j++)
    15         {
    16             or2[i]+=(wqw[j]*(tat[j][i]))%k;
    17             or2[i]%=k;
    18         }
    19     }
    20     memcpy(wqw,or2,sizeof(or2));
    21 }
    22 void mulself(long long awa[10][10])
    23 {
    24     long long or3[10][10];
    25     memset(or3,0,sizeof(or3));
    26     for(int i = 0; i < 10 ; i++)
    27     {
    28         for(int j = 0 ; j< 10 ; j++)
    29         {
    30             for(int kk = 0 ; kk < 10 ; kk++)
    31             {
    32                 or3[i][j]+=((awa[i][kk])%k)*((awa[kk][j])%k)%k;
    33                 or3[i][j]%=k;
    34             }
    35         }
    36     }
    37     memcpy(awa,or3,sizeof(or3));
    38 }
    39 int main()
    40 {
    41     while(scanf("%lld%lld",&n,&k)==2)
    42     {
    43     
    44     long long qaq[10][10];
    45     long long qwq[10];
    46     long long orz[10]={9,8,7,6,5,4,3,2,1,0};
    47     memset(qaq,0,sizeof(qaq));
    48     for(int i = 0 ; i < 10 ; i++)
    49     {
    50         scanf("%lld",&qaq[i][0]);
    51     }
    52     for(int i = 1 ; i < 10; i++)
    53     {
    54         qaq[i-1][i]=1;
    55     }
    56     if(n<10)
    57     {
    58         printf("%lld
    ",n%k);
    59     }
    60     else
    61     {
    62         n=n-9;
    63         while(n)
    64         {
    65             if(n&1)mul(orz,qaq);
    66             mulself(qaq);
    67             n/=2;
    68         }
    69         cout  <<orz[0]%k<<endl;
    70     }
    71 }
    72     return 0;
    73 }
  • 相关阅读:
    PHP计算近1年的所有月份
    mysql的索引和锁
    深度解析 https 协议
    linux 常用命令大全
    为什么Python3.6字典变得有序了?
    oddo
    RESTful接口开发规范
    python中的 __inti__ 和 __new__ 方法的区别
    十大经典算法 Python实现
    MongoDB journal 与 oplog,究竟谁先写入?--转载
  • 原文地址:https://www.cnblogs.com/MekakuCityActor/p/9008139.html
Copyright © 2020-2023  润新知