HDU 1757 a simple math problem matrix fast power

Source: Internet
Author: User

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 );

For such a formula, you can easily construct such a matrix through the relationship between the matrix and linear transformation.

A0:

9
8
7
6
5
4
3
2
1
0

A1:
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0

Then f (n) = A1 ^ (N-9) * a0

Quick power

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <climits>#include <iostream>#include <string>using namespace std; #define MP make_pair#define PB push_backtypedef long long LL;typedef unsigned long long ULL;typedef vector<int> VI;typedef pair<int, int> PII;typedef pair<double, double> PDD;const int INF = INT_MAX / 3;const double eps = 1e-8;const LL LINF = 1e17;const double DINF = 1e60;const int maxn = 11;LL k, mod;struct Matrix {    int n, m;    LL data[maxn][maxn];    Matrix(int n = 0, int m = 0): n(n), m(m) {        memset(data, 0, sizeof(data));    }};Matrix operator * (Matrix a, Matrix b) {    Matrix ret(a.n, b.m);    for(int i = 1; i <= a.n; i++) {        for(int j = 1; j <= b.m; j++) {            for(int k = 1; k <= a.m; k++) {                ret.data[i][j] += a.data[i][k] * b.data[k][j];                ret.data[i][j] %= mod;            }        }    }    return ret;}Matrix pow(Matrix mat, LL p) {    if(p == 1) return mat;    Matrix ret = pow(mat * mat, p / 2);    if(p & 1) ret = ret * mat;    return ret;}int main() {    int a[10];    while(cin >> k >> mod) {        for(int i = 0; i < 10; i++) cin >> a[i];        Matrix A(10, 10), A0(10, 1);        if(k < 10) cout << k % mod << endl;        else {            for(int i = 1; i <= 10; i++) A.data[1][i] = a[i - 1];            for(int i = 2; i <= 10; i++) A.data[i][i - 1] = 1;            for(int i = 1, j = 9; i <= 10; i++, j--) A0.data[i][1] = j;            A = pow(A, k - 9);            A0 = A * A0;            cout << A0.data[1][1] << endl;        }    }    return 0;}

  

 

HDU 1757 a simple math problem matrix fast power

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.