HDU 5015 233 matrix (constructor matrix)

Source: Internet
Author: User
Tags mul

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5015


Because it is a two-dimensional recursive formula, we didn't expect to construct a matrix like this. From the column, the current column is obtained by recursion of the previous column. Construct a matrix based on this point. B [I] represents column I, which is a matrix (n + 2) * 1, that is, B [1] = [1,233 ......], the reason why two rows are added is to obtain the second number 2333 in B [I] from a matrix B [I-1 ..., construct a conversion matrix A, which is a matrix (n + 2) * (N + 2). Then a ^ m-1) * B is the m column.


/* A matrix: 1 0 0 0 0... 3 10 0 0 0... 3 10 1 0 0... 3 10 1 1 0... 3 10 1 1 1 ..... matrix B: 1st columns */# include <stdio. h> # include <iostream> # include <map> # include <set> # include <list> # include <stack> # include <vector> # include <math. h> # include <string. h> # include <queue> # include <string> # include <stdlib. h >#include <algorithm> # define ll _ int64 // # define ll long # define EPS 1e-9 # define PI ACOs (-1.0) using namespace STD; Co NST int INF = 0x3f3f3f; const int mod = 10000007; struct matrix {ll mat [15] [15]; void Init () {memset (MAT, 0, sizeof (MAT); For (INT I = 0; I <15; I ++) {mat [I] [I] = 1 ;}} A, B, res; int n, m; matrix MUL (matrix A, matrix B) {matrix ans; memset (ans. mat, 0, sizeof (ans. mat); For (INT I = 0; I <n + 2; I ++) {for (int K = 0; k <n + 2; k ++) {if (. mat [I] [k] = 0) continue; For (Int J = 0; j <n + 2; j ++) {ans. mat [I] [J] + = (. mat [I] [k] * B. mat [k] [J]) % MOD; ans. mat [I] [J] % = mod ;}} return ans;} matrix POW (matrix A, int N) {matrix ans; ans. init (); While (n) {If (N & 1) ans = MUL (ANS, a); N >>= 1; A = MUL (a, );} return ans;} int main () {int X; while (~ Scanf ("% d", & N, & M) {memset (B. mat, 0, sizeof (B. mat); B. mat [0] [0] = 1; B. mat [1] [0] = 233; For (INT I = 2; I <n + 2; I ++) {scanf ("% d", & X ); b. mat [I] [0] = (B. mat [I-1] [0] + x % mod) % MOD;} memset (. mat, 0, sizeof (. mat);. mat [0] [0] = 1; for (INT I = 1; I <n + 2; I ++) {. mat [I] [0] = 3;. mat [I] [1] = 10; for (Int J = 2; j <= I; j ++). mat [I] [J] = 1;} res = POW (A m-1); ll anw = 0; For (INT I = 0; I <n + 2; I ++) {ANW + = (res. mat [n + 1] [I] * B. mat [I] [0]) % MOD; ANW % = MOD;} printf ("% i64d \ n", ANW);} return 0 ;}


HDU 5015 233 matrix (constructor matrix)

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.