`
Simone_chou
  • 浏览: 192642 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

A Simple Math Problem(矩阵快速幂)

    博客分类:
  • HDOJ
 
阅读更多

A Simple Math Problem

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


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

      

      题意:

      给出 k( < 2 X 10 ^ 9) 和 m( < 10 ^ 5),数列的规则:

      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);
      求出 f(k)%m 。

 

      思路:

      矩阵快速幂。根据式子可以得出:

 

化简后可以得出 :

 


 

 

      AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

typedef vector<int> vec;
typedef vector<vec> met;

met mul (met a, met b, int mod) {
    met c(a.size(), vec(b[0].size()));

    for (int i = 0; i < a.size(); ++i) {
        for (int j = 0; j < b[0].size(); ++j) {
            for (int k = 0; k < b.size(); ++k) {
                c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
            }
        }
    }

    return c;
}

met pow (met a, int n, int mod) {

    met b(a.size(), vec(a[0].size()));
    for (int i = 0; i < a.size(); ++i) {
        b[i][i] = 1;
    }

    while (n > 0) {
        if (n & 1) b = mul(b, a, mod);
        a = mul(a, a, mod);
        n >>= 1;
    }

    return b;
}

int main () {
    int k, mod;

    while (~scanf("%d%d", &k, &mod)) {
        met a(10, vec(10));

        for (int i = 0; i < a[0].size(); ++i)
            scanf("%d", &a[0][i]);

        for (int i = 1; i < a.size(); ++i)
            a[i][i - 1] = 1;

        if (k >= 10) {
                int sum = 0;
                a = pow(a, k - 9, mod);

                for (int i = 0; i < 10; ++i) {
                    sum = (sum + a[0][i] * (9 - i)) % mod;
                }

                printf("%d\n", sum % mod);
        } else printf("%d\n", k % mod);

    }

    return 0;
}

 

 

  • 大小: 561.8 KB
  • 大小: 575.8 KB
分享到:
评论

相关推荐

    Simple Math Challenge App

    "Simple Math Challenge App" 是一个基于JavaScript开发的数学游戏应用,旨在通过互动的方式提升用户的数学技能,特别是对基本算术运算的理解和速度。这个应用程序很可能包含HTML、CSS和JavaScript文件,构建了一个...

    Simple Math Trainer Game

    "Simple Math Trainer Game" 是一个基于JavaScript开发的小型数学训练游戏。这个项目旨在帮助用户提高基本的算术技能,如加、减、乘、除,同时也是一个很好的实践JavaScript编程技巧的例子。下面我们将深入探讨这个...

    Simple Math Game Application with SQLite in Python

    在本项目"Simple Math Game Application with SQLite in Python"中,我们将探讨如何利用Python语言和SQLite数据库来构建一个简单的数学游戏应用。这个应用可能包括基础的加减乘除运算,通过与用户交互,提升用户对...

    Simple Math Application

    "Simple Math Application" 是一个基于JavaScript开发的数学应用,它可能是设计用来帮助用户进行基本的数学练习或测试用户计算技能的。JavaScript是一种广泛使用的编程语言,尤其在网页开发中,它可以为网页添加交互...

    A Simple Mesh Generator in MATLAB

    A Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLAB

    Python库 | simple_math-1.0.0.tar.gz

    本资源是一个名为"simple_math"的Python库,版本为1.0.0,以`.tar.gz`格式打包。这个库很可能包含了方便进行基础数学运算的函数和类,旨在简化开发者的代码,提高开发效率。 首先,让我们来了解Python库的基本概念...

    A SIMPLE MESH GENERATOR IN MATLAB

    We want to offer a short and simple MATLAB code, described in more detail than usual, so the reader can experiment (and add to the code) knowing the underlying principles. We find the node locations ...

    airavata-simple-math-service-0.10.jar

    标签:airavata-simple-math-service-0.10.jar,airavata,simple,math,service,0.10,jar包下载,依赖包

    airavata-simple-math-service-0.6.jar

    标签:airavata-simple-math-service-0.6.jar,airavata,simple,math,service,0.6,jar包下载,依赖包

    airavata-simple-math-service-0.8.jar

    标签:airavata-simple-math-service-0.8.jar,airavata,simple,math,service,0.8,jar包下载,依赖包

    airavata-simple-math-service-0.11.jar

    标签:airavata-simple-math-service-0.11.jar,airavata,simple,math,service,0.11,jar包下载,依赖包

    airavata-simple-math-service-0.7.jar

    标签:airavata-simple-math-service-0.7.jar,airavata,simple,math,service,0.7,jar包下载,依赖包

    airavata-simple-math-service-0.5.jar

    标签:airavata-simple-math-service-0.5.jar,airavata,simple,math,service,0.5,jar包下载,依赖包

    airavata-simple-math-service-0.9.jar

    标签:airavata-simple-math-service-0.9.jar,airavata,simple,math,service,0.9,jar包下载,依赖包

    SIMPLE算法-MATLAB.zip_simple_simple matlab_simple 算法_simple-matlab

    在MATLAB环境中实现SIMPLE算法,需要对矩阵运算、循环结构以及迭代收敛判断有深入理解。MATLAB提供的工具如`fsolve`或自定义迭代函数可用于解决非线性问题。`pdepe`函数也可以用于偏微分方程的求解,可能在处理压力...

    airavata-simple-math-service-0.9-javadoc.jar

    标签:airavata-simple-math-service-0.9-javadoc.jar,airavata,simple,math,service,0.9,javadoc,jar包下载,依赖包

    airavata-simple-math-service-0.6-sources.jar

    标签:airavata-simple-math-service-0.6-sources.jar,airavata,simple,math,service,0.6,sources,jar包下载,依赖包

    airavata-simple-math-service-0.6-javadoc.jar

    标签:airavata-simple-math-service-0.6-javadoc.jar,airavata,simple,math,service,0.6,javadoc,jar包下载,依赖包

    airavata-simple-math-service-0.7-sources.jar

    标签:airavata-simple-math-service-0.7-sources.jar,airavata,simple,math,service,0.7,sources,jar包下载,依赖包

    airavata-simple-math-service-0.10-sources.jar

    标签:airavata-simple-math-service-0.10-sources.jar,airavata,simple,math,service,0.10,sources,jar包下载,依赖包

Global site tag (gtag.js) - Google Analytics