HDU1757:A Simple Math Problem(矩阵快速幂)

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

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

题目解析:

前面已经写了一篇博客如何构造矩阵,这道题可以说就是上片博客的简单应用。

矩阵的乘法不满足交换律,但是却满足结合律,如:A*B*C=A*(B*C);

f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10)
构造的矩阵是:
|0 1 0 ......... 0|    |f0|   |f1 |
|0 0 1 0 ...... 0|    |f1|   |f2 |
|...................1| *  |..| = |...|
|a9 a8 .......a0|    |f9|   |f10|

然后根据矩阵的结合律,可以先把构造的矩阵的K次幂求出来。最后直接求第一个数。

代码:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct ma
{
    int a[10][10];
} init,res;
int K;
int mod,b[10],f[10];
ma Mul(ma x,ma y)
{
    ma tmp;
    for(int i=0; i<10; i++)
        for(int j=0; j<10; j++)
        {
            tmp.a[i][j]=0;
            for(int k=0; k<10; k++)
                tmp.a[i][j]=(tmp.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
        }
    return tmp;
}
ma Pow(ma x,int K)
{
    ma tmp;
    for(int i=0; i<10; i++)
    {
        for(int j=0; j<10; j++)
            tmp.a[i][j]=(i==j);
    }
    while(K!=0)
    {
        if(K&1)
            tmp=Mul(tmp,x);
        K>>=1;
        x=Mul(x,x);
    }
    return tmp;
}
int main()
{
    while(scanf("%d%d",&K,&mod)!=EOF)
    {
        for(int i=0; i<=9; i++)
        {
            scanf("%d",&init.a[9][9-i]);
        }
        if(K<=9)
        {
            printf("%d\n",K);
            continue;
        }
        for(int i=0; i<10; i++)
            f[i]=i;
        for(int i=0; i<=8; i++)
        {
            for(int j=0; j<=9; j++)
                init.a[i][j]=(i==j-1);
        }
        res=Pow(init,K);
        int ans=0;
        for(int j=0; j<10; j++)
        {
            ans=(ans+res.a[0][j]*j)%mod;
        }
        printf("%d\n",ans);

    }
    return 0;
}

加深印象,写了两次。

#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct ma
{
    int a[10][10];
} init,res;
int K;
int mod,b[10],f[10];
ma Mul(ma x,ma y)
{
    ma tmp;
    for(int i=0; i<10; i++)
        for(int j=0; j<10; j++)
        {
            tmp.a[i][j]=0;
            for(int k=0; k<10; k++)
                tmp.a[i][j]=(tmp.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
        }
    return tmp;
}
ma Pow(ma x,int K)
{
    ma tmp;
    for(int i=0; i<10; i++)
    {
        for(int j=0; j<10; j++)
            tmp.a[i][j]=(i==j);
    }
    while(K!=0)
    {
        if(K&1)
            tmp=Mul(tmp,x);
        K>>=1;
        x=Mul(x,x);
    }
    return tmp;
}
int main()
{
    while(scanf("%d%d",&K,&mod)!=EOF)
    {
        for(int i=0; i<=9; i++)
        {
            scanf("%d",&init.a[9][9-i]);
        }
        if(K<=9)
        {
            printf("%d\n",K);
            continue;
        }
        for(int i=0; i<10; i++)
            f[i]=i;
        for(int i=0; i<=8; i++)
        {
            for(int j=0; j<=9; j++)
                init.a[i][j]=(i==j-1);
        }
        res=Pow(init,K-9);
        int ans=0;
        for(int j=0; j<10; j++)
        {
            ans=(ans+(res.a[9][j])*f[j])%mod;
        }
        printf("%d\n",ans);

    }
    return 0;
}

时间: 2024-08-04 08:19:08

HDU1757:A Simple Math Problem(矩阵快速幂)的相关文章

hdu1757 A Simple Math Problem(矩阵快速幂)

题目: A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3522    Accepted Submission(s): 2130 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 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 w

hdu-1757 A Simple Math Problem---矩阵快速幂模板题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m 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 . 解题思路: 构建矩阵 直接用矩阵快速幂模板求解 注意,小于10的时候不能直接输出

BestCoder Round #29——A--GTY&#39;s math problem(快速幂(对数法))、B--GTY&#39;s birthday gift(矩阵快速幂)

GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description GTY is a GodBull who will get an Au in NOI . To have more time to learn alg

hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2791    Accepted Submission(s): 1659 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) =

hdu 1757 A Simple Math Problem 矩阵

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2831    Accepted Submission(s): 1693 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x)

A Simple Math Problem 矩阵打水题

A Simple Math Problem 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 pos

LightOJ 1070 - Algebraic Problem 矩阵快速幂

题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessar

HDU 4291 A Short problem(矩阵快速幂+循环节)

题目链接": http://acm.hdu.edu.cn/showproblem.php?pid=4291 题意: g(0)=0,g(1)=1; g(n) = 3g(n - 1) + g(n - 2): 求g(g(g(n))) mod 109 + 7 分析: 首先我们得认识到,如果一层一层算是必定会超时的. 其次,取模运算是有循环节的. step1我们找出g(x)%1000000007的循环节 mod1 step2 设g(g(n)) = g(x) x=g(n) 对mod1 取模得到mod2. 剩

hdu 1757 A Simple Math Problem 矩阵优化+快速幂

#include<bits/stdc++.h> using namespace std; typedef long long LL; const int n=10; int kt,m; struct Matrix { int mp[n][n]; }; Matrix mul(Matrix a,Matrix b) { int i,j,k; Matrix c; for(i=0; i<10; i++) for(j=0; j<10; j++) { c.mp[i][j]=0; for(k=0;