码迷,mamicode.com
首页 > 其他好文 > 详细

Hdoj 1757

时间:2018-01-31 18:34:42      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:case   fine   names   name   func   fun   positive   about   amp   

描述

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.

输入

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.

输出

For each case, output f(k) % m in one line.

样例输入

10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0

样例输出

45
104

思路

很基础的矩阵快速幂

代码

#include <cstdio>
#include<cstring>
#define ll long long
#define maxn 10
using namespace std;

int k, mod;

struct Mat
{
    ll f[maxn][maxn];
    void cls(){memset(f, 0, sizeof(f));}//全部置为0 
    Mat() {cls();}
    friend Mat operator * (Mat a, Mat b)
    {
        Mat res;
        for(int i = 0; i < maxn; i++) for(int j = 0; j < maxn; j++)
            for(int k = 0; k < maxn; k++)
                (res.f[i][j] += a.f[i][k] * b.f[k][j]) %= mod;
        return res;
    }
};

Mat quick_pow(Mat a)  
{  
    Mat ans;
    for(int i = 0; i < maxn; i++) ans.f[i][i] = 1;
    int b = k;
    while(b != 0)  
    {
        if(b & 1) ans = ans * a;
        b >>= 1;
        a = a * a;
    }
    return ans;  
}

int main()
{
    Mat A, B;
    for(int i = 0; i < 10; i++)
        B.f[0][i] = 9 - i;
    for(int i = 1; i < 10; i++) A.f[i-1][i] = 1;
    while(~scanf("%d %d", &k, &mod))
    {
        Mat C;
        for(int i = 0; i < 10; i++) scanf("%d", &A.f[i][0]);
        if(k < 10) {printf("%d\n", k % mod); continue;}
        k -= 9;
        C = quick_pow(A);
        C = B * C;
        printf("%d\n", C.f[0][0]);
    }
    return 0;
}

Hdoj 1757

标签:case   fine   names   name   func   fun   positive   about   amp   

原文地址:https://www.cnblogs.com/HackHarry/p/8392106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!