Topic Link: Poke Me
Main topic:
A function f (x) when x < ten, f (x) = x;
When x >= 10 o'clock, f (x) = a0 * F (X-1) + A1 * F (x-2) + A2 * F (x-3) + ... + A9 * F (x-10);
For K and M, ask for the value f (k)% m
Sample explanation:
Slightly
Problem Solving Ideas:
A matrix of 10 * 10 is established with a matrix fast power, as follows
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
A[9] a[8] a[7] a[6] a[5] a[4] a[3] a[2] a[1] a[0] Where a array is a corresponding to the above formula
The b matrix is 10 * 1, as follows
[0;1;2;3;4;5;6;7;8;9]
So f (n) is the last element of the matrix derived from an * B
Code:
Author ljh//www.cnblogs.com/tenlee#include <cstdio> #include <cstdlib> #include <cstring># Include <cctype> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <map> #define CLC (A, B) memset (A, B, sizeof (a)) using namespace Std;const int inf = 0x3 f;const int INF = 0x3f3f3f3f;const int maxn = 10;int k, m;void Init (int a[][maxn], int ans[][maxn]) {for (int i = 0; I & Lt 9; i++) {for (int j = 0; J <; J + +) {A[i][j] = 0; if (j-1 = = i) {a[i][j] = 1; }}} for (int i = 0; i < MAXN; i++) {for (int j = 0; J < Maxn; J + +) {I F (i = = j) Ans[i][j] = 1; else ans[i][j] = 0; }}}void Matrix_mul (int a[][maxn], int b[][maxn]) {int I, j, K; int TMP[MAXN][MAXN] = {0}; for (i = 0; i < MAXN; i++) {for (j = 0; J < Maxn; J + +) {for (k = 0; k < maxn; k++) {Tmp[i][j] = (Tmp[i][j] + a[i][k] * b[k][ J]% m)% m; }}} for (i = 0; i < MAXN; i++) {for (j = 0; J < Maxn; J + +) {A[i][j] = tmp I [j]; }}/*for (int i = 0; i < MAXN; i++) {for (int j = 0; J < Maxn; J + +) printf ("B%d", a[i][ J]); Puts (""); }*/}int quickpow (int a[maxn][maxn], int b[maxn], int ans[maxn][maxn], int n) {/*for (int i = 0; i < MAXN; i++) { for (int j = 0; J < Maxn; J + +) printf ("%d", a[i][j]); Puts (""); }*/while (n) {if (n & 1) Matrix_mul (ans, a); Matrix_mul (A, a); n >>= 1; }/*for (int i = 0; i < MAXN; i++) {for (int j = 0; J < Maxn; J + +) printf ("%d", ans[i][j]); Puts (""); }*/int sum = 0; for (int i = 0; i < MAXN; i++) {sum = (sum +I * ans[9][i])% m; } return sum% m;} void func (int a[][maxn]) {int d[50]; for (int i = 0; i < i++) d[i] = i; for (int i = ten; I <= K; i++) {d[i] = 0; for (int j = 0; J <; J + +) {D[i] = (D[i] + a[9][j] * d[i-j-1]% m)% m; } printf ("%d#%d", I, d[i]); } puts ("");} int main () {freopen ("1.txt", "R", stdin); int A[MAXN][MAXN], B[MAXN], ANS[MAXN][MAXN]; while (~scanf ("%d%d\n", &k, &m)) {Init (A, ans); for (int i = 0; i < i++) {scanf ("%d", &a[9][9-i]); }//func (a); if (k >=) printf ("%d\n", Quickpow (A, B, ans, k-9)); else printf ("%d\n", k%m); } return 0;}
HDU 1757 A simple Math problem (Matrix fast power)