Hdoj ARC of the Dream 4686 "Matrix fast Power"

Source: Internet
Author: User



ARC of DreamTime limit:2000/2000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 3126 Accepted Submission (s): 982


Problem Descriptionan ARC of Dream is a curve defined by following function:

where
a0 = A0
AI = Ai-1*ax+ay
B0 = B0
BI = Bi-1*bx+by
What is the value of AoD (N) modulo 1,000,000,007?
Inputthere is multiple test cases. Process to the End of File.
Each test case contains 7 nonnegative integers as follows:
N
A0 AX AY
B0 BX by
N is no further than 1018, and all the other integers be no more than 2x109.
Outputfor each test case, output AoD (N) modulo 1,000,000,007.
Sample Input
11 2 34 5 621 2 34 5 631 2 34 5 6

Sample Output
41341902

Authorzejun Wu (Watashi)
Source2013 multi-university Training Contest 9
Recommendzhuyuanchen520 | We have carefully selected several similar problems for you:5390 5389 5388 5387 5386


Test instructions: Look at the formula .... Ai*bi the sum of the first n (0-n-1) of the series.

Problem-Solving ideas: 10^18 the second party. Decisive use of matrix fast power. The key is to find the matrix of multiplication.

1

0

0

1

0

0

Ax

0

0

Ay

0

0

Bx

0

By

0

Ax*by

Bx*ay

Ax*bx

Ay*by

0

0

0

0

1








is to use the matrix above to multiply

Si-1

Ai-1

Bi-1

Ai-1*bi-1

1








The result of multiplying them is

Si

Ai

Bi

Ai*bi

1












Therefore, the first matrix is directly to the N-order fast power. Multiply the second matrix, get the last matrix, and then output the first element.


AC Code:

#include <stdio.h> #include <math.h> #include <vector> #include <queue> #include <string> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm>using Namespace Std;typedef long Long ll;const int maxn = 5;const LL modn = 1000000007; LL RES[MAXN][MAXN]; LL A[MAXN][MAXN];    ll A0,ax,ay,b0,bx,by;void Matmul (ll x[maxn][maxn],ll Y[maxn][maxn]) {ll t[maxn][maxn]={0};             for (int i=0;i<maxn;i++) {to (int k=0;k<maxn;k++) if (X[i][k]) for (int j=0;j<maxn;j++)    t[i][j]= (T[I][J]+X[I][K]*Y[K][J]%MODN)%modn; } for (int i=0;i<maxn;i++) for (int j=0;j<maxn;j++) x[i][j]=t[i][j];} void Matrix (LL x[maxn][maxn],ll N) {for (Int. i=0;i<maxn;i++) for (int j=0;j<maxn;j++) res[i][j]= (i==j    );        while (n) {if (n&1) Matmul (res,x);        Matmul (X,X);    n>>=1;    }}void init () {memset (a,0,sizeof (a));    A[0][0]=a[0][3]=1; a[1][1]=ax%MODN;A[1][4]=AY%MODN;    A[2][2]=BX%MODN;A[2][4]=BY%MODN;    A[3][1]=AX*BY%MODN;A[3][2]=BX*AY%MODN;    A[3][3]=AX*BX%MODN;A[3][4]=AY*BY%MODN; A[4][4]=1;}    int main () {LL n,s;        while (scanf ("%lld", &n)!=eof) {LL ans[5];        scanf ("%lld%lld%lld%lld%lld%lld", &a0,&ax,&ay,&b0,&bx,&by);            if (n==0) {printf ("0\n");        Continue        } ans[0]=0;ans[1]=a0;ans[2]=b0;ans[3]=a0*b0%modn;ans[4]=1;        Init ();        Matrix (A,n);        s=0;        for (int i=0;i<maxn;i++) {s= (S+ANS[I]*RES[0][I]%MODN)%modn;    } printf ("%lld\n", S); } return 0;}


Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

Hdoj ARC of the Dream 4686 "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.