hdu1757 A simple Math problem (Matrix fast power)

Source: Internet
Author: User

Topic:

A Simple Math problem Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3522 Accepted Submission (s): 2130


Problem Descriptionlele Now was thinking about a simple function f (x).

If x < f (x) = x.
If x >= 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'll give A0 ~ A9 and positive integers k and M, and could you help Lele to Caculate f (k)%m.

Inputthe problem contains mutiple test cases. Please process to the end of file.
In each case, there'll be is the lines.
In the first line, there is, positive integers k and M. (K<2*10^9, M < 10^5)
In the second line, there is ten integers represent A0 ~ A9.

Outputfor per case, Output f (k)% m on one line.
Sample Input
10 99991 1 1 1 1 1 1 1 1 120 5001 0 1 0 1 0 1 0 1 0

Sample Output
45104

Authorlinle
Source2007 Provincial Training Team Practice (6) _linle session
Recommendlcy

Test instructions: Give the recursive formula of a function to find out its K term.

Idea: Matrix fast power.

Code:

#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath > #include <climits> #include <algorithm> #include <vector> #include <string> #include < iostream> #include <sstream> #include <map> #include <set> #include <queue> #include < stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> Using namespace std, #define PB push_back#define MP make_pair#define REP (i,x,n) for (int i=x;i< (n); ++i) #define for (I,l, h) for (int i= (l); i<= (h), ++i) #define FORD (i,h,l) for (int i= (h); i>= (l); i) #define SZ (x) ((int) (x). Size ()) #define All (x) (x). Begin (), (x). End () #define RI (x) scanf ("%d", & (x)) #define RII (x, Y) scanf ("%d%d", & (X), & (Y)) # Define RIII (x, Y, z) scanf ("%d%d%d", & (X), & (Y), & (z)) #define DRI (x) int (x); scanf ("%d", &x) #define DRII (x, y) int X, y; scanf ("%d%d", &x, &y) #define DRIII (x, y, z) int x, y, Z; scanf ("%d%d%d", &x, &y, &z) #define OI (x) printf ("%d", x), #define RS (x) scanf ("%s", (x)) #define MS0 (x) memset ( (x), 0, sizeof ((x))) #define MS1 (x) memset ((x),-1, sizeof ((x))) #define LEN (x) strlen (x) #define F first#define S second#def  Ine Swap (A, B) (a ^= B, b ^= A, a ^= b) #define Dpoint strcut node{int x, y} #define CMPD int cmp (const int &AMP;A,CONST int    &AMP;B) {return a>b;}/* #ifdef HOME freopen ("In.txt", "R", stdin); #endif */const int MOD = 1e9+7;typedef vector<int> vi;typedef vector<string> vs;typedef vector<double> Vd;typedef Long Long ll;typedef pair<int,int> pii;//#define Homeint Scan () {int res = 0, ch, flag = 0;if (ch = getcha R ()) = = '-')//determine positive and negative flag = 1;else if (ch >= ' 0 ' && ch <= ' 9 ')//Get complete number res = CH-' 0 '; while ((ch = getchar ()) > = ' 0 ' && ch <= ' 9 ') res = res * + CH-' 0 '; return flag? -res:res;}/*----------------Please-----does-----not-----HACK-----ME--------------------*/struct matrix{int m[15][15];    void Init () {MS0 (M); }};    Matrix Mul (Matrix A,matrix b,int M) {matrix C;    C.init (); for (int i=0;i<10;i++) for (int j=0;j<10;j++) for (int k=0;k<10;k++) {c.m[i][j]+= (long long) a.m[i    ][K]*B.M[K][J])%M;    C.m[i][j]%=m;} return c;}    Matrix Mypow (Matrix A,int k,int M) {matrix ans;    Ans.init ();    for (int i=0;i<10;i++) ans.m[i][i]=1;    Matrix Temp=a;        while (k) {if (k&1) Ans=mul (ans,temp,m);        k>>=1;    Temp=mul (TEMP,TEMP,M); } return ans;        int A[10];int Main () {int k,m;while (RII (k,m)!=eof) {if (k<10) {printf ("%d\n", k%m);    Continue    } for (int i=0;i<10;i++) RI (A[i]);    Matrix A;    A.init ();    for (int i=0;i<10;i++) a.m[0][i]=a[i];    for (int i=1;i<10;i++) a.m[i][i-1]=1;    Matrix Res=mypow (a,k-9,m);   int ans=0; for (int i=0;i<10;i++) ans= (ans+ (Long Long) res.m[0][i]* (9-i))%m;        printf ("%d\n", ans);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1757 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.