#include <iostream> #include <cstdio> #include <cstring> #define REP(i, s, n) for(int i = s; i <= n; i ++) #define REP_(i, s, n) for(int i = n; i >= s; i --) #define MAX_N 10 + 5 using namespace std; int k, m; struct node{ int Mtx[MAX_N][MAX_N]; }med; void Pre_Set(){ REP(i, 1, 10) scanf("%d", &med.Mtx[1][i]); REP(i, 2, 10) REP(j, 1, 10){ if(i - j == 1) med.Mtx[i][j] = 1; else med.Mtx[i][j] = 0; } } node operator*(node a,node b){ node c; REP(i, 1, 10) REP(j, 1, 10){ c.Mtx[i][j] = 0; REP(k, 1, 10) c.Mtx[i][j] += a.Mtx[i][k] * b.Mtx[k][j]; c.Mtx[i][j] %= m; } return c; } node operator^(node a,int k){ if(k == 0){ memset(a.Mtx, 0, sizeof(a.Mtx)); REP(i, 1, 10) a.Mtx[i][i] = 1; return a; } if(k == 1) return a; node c = a ^ (k >> 1); if(k & 1) return c * c *a; return c * c; } int main(){ while(scanf("%d%d", &k, &m) != EOF){ Pre_Set(); if(k < 10) { cout << k % m << endl; } else { med = med ^ (k - 9); int ans = 0; REP(i, 1, 10){ ans = (ans + med.Mtx[1][i] * (10 - i)) % m; } printf("%d\n", ans); } } return 0; }
HDU 1757 A Simple Math Problem
原文:http://www.cnblogs.com/ALXPCUN/p/4590403.html