HDU 1757-a simple Math problem (Matrix fast power)

Source: Internet
Author: User

A Simple Math problemTime limit:MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u SubmitStatusPracticeHDU 1757Appoint Description:System Crawler (2015-03-04)

Description

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

Input

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

Output

For each case, output f (k)% m in one line.

Sample Input

Sample Output


Test instructions: It's two formulas.
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); evaluated.

Idea: This is a preliminary learning how to construct a matrix, in fact, there is nothing rare, mainly to think bad, alas, or too weak.

X<10 time to say, the following is the situation of n>=10. Such as

Matrix A matrix B


0 1 0 0 0 0 0 0 0 0 F0 F1


0 0 1 0 0 0 0 0 0 0 F1 f2


0 0 0 1 0 0 0 0 0 0 F2 f3


0 0 0 0 1 0 0 0 0 0 * F3----------> F4


0 0 0 0 0 1 0 0 0 0 f4 f5


0 0 0 0 0 0 1 0 0 0 f5 f6


0 0 0 0 0 0 0 1 0 0 f6 F7


0 0 0 0 0 0 0 0 1 0 F7 F8


0 0 0 0 0 0 0 0 0 1 F8 F9


A9 A8 A7 a6 A5 A4 A3 A2 A1 A0 F9 F10


We see the law, every time to the next a*b, and so on by a*a*a ... A*b;


#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <algorithm> #include <set> #include <map> #include <queue>using namespace std;const int Inf=0x3f3f3f3f;int mod;int a[20];struct node {int mp[20][20];} init,res;struct node Mult (struct node x,s    Truct node y) {struct node tmp;    int i,j,k;            For (i=0, i<10; i++) for (j=0; j<10; J + +) {tmp.mp[i][j]=0;            for (k=0; k<10; k++) {tmp.mp[i][j]= (tmp.mp[i][j]+x.mp[i][k]*y.mp[k][j])%mod; }} return tmp;};    struct Node Expo (struct node X,int k) {struct node tmp;    int i,j;            For (i=0, i<10; i++) for (j=0; j<10; J + +) {if (i==j) tmp.mp[i][j]=1;        else tmp.mp[i][j]=0;        } while (k) {if (k&1) Tmp=mult (tmp,x);        X=mult (X,X);    k>>=1; } return tmp;}; int main () {int k,x,i,j;    while (~SCANF ("%d%d", &k,&mod)) {if (k<10) {printf ("%d\n", k%mod);        Continue;        } for (i=9; i>=0; i--) a[i]=9-i;            for (i=0; i<10; i++) {scanf ("%d", &x);        Init.mp[0][i]=x%mod; } for (I=1, i<10; i++) {for (j=0; j<10; J + +) {if (i==j+1) init.mp I                [J]=1;            else init.mp[i][j]=0;        }} res=expo (init,k-9);        int ans=0;        for (j=0; j<10; J + +) {ans= (ans+res.mp[0][j]*a[j])%mod;    } printf ("%d\n", ans); } 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.