Hdu 4686 ARC of Dream (Matrix fast power multiplication)

Source: Internet
Author: User
Tags mul

problem Description
 is a curve defined by following function:



where== ai-1*ax+== bi-1*bx+ is 1,007?

Input
 Case 7  as  is10182x109.

Output
 Case 1,007,and so on.

Sample Input
1 1 2 3 4 5 6 2 1 2 3 4 5 6 3 1 2 3 4 5 6

Sample Output
4 134 1902

Author Zejun Wu (Watashi)

Source multi-university Training Contest 9

because: a[i]*b[i]= (a[i-1]*ax+ay) * (B[i-1]*bx+by)

= (A[i-1]*b[i-1]*ax*bx+a[i-1]*ax*by+b[i-1]*bx*ay+ay*by)

Construct The matrix:

| 1 0 0 0 0 |

| Ax*by AX 0 Ax*by 0 |

{AoD (n-1), a[i-1],b[i-1],a[i-1]*b[i-1],1}* |      Bx*ay 0 BX Bx*ay 0 | ={aod (n), a[i],b[i],a[i]*b[i],1}

| AX*BX 0 0 AX*BX 0 |

| Ay*by AY by Ay*by 1 |

Also note:

if (n==0) {//This judging condition is very important, it will not be timed out
printf ("0\n");
Continue
}

1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - #definePI ACOs (-1.0) - #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long - #defineEPS 1e-10 + #defineMOD 1000000007 A #defineN 1000000 at #defineINF 1e12 - ll N; - ll A0,ax,ay,b0,bx,by; - structmatrix{ -ll mp[5][5]; - }; in Matrix Mul (Matrix A,matrix b) { - Matrix Res; to     for(LL i=0;i<5; i++){ +        for(LL j=0;j<5; j + +){ -res.mp[i][j]=0; the           for(LL k=0;k<5; k++){ *Res.mp[i][j]= (res.mp[i][j]+ (a.mp[i][k]*b.mp[k][j])%mod+mod)%MOD; $          }Panax Notoginseng       } -    } the    returnRes; + } A Matrix fastm (Matrix A,ll b) { the Matrix Res; +memset (RES.MP,0,sizeof(RES.MP)); -     for(LL i=0;i<5; i++){ $res.mp[i][i]=1; $    } -     while(b) { -       if(b&1){ theres=Mul (res,a); -       }WuyiA=Mul (a,a); theb>>=1; -    } Wu    returnRes; - } About intMain () $ { -     while(SCANF ("%i64d", &n) = =1){ -scanf"%i64d%i64d%i64d%i64d%i64d%i64d",&a0,&ax,&ay,&b0,&bx,&by ); -        A       if(n==0){//This condition of judgment is very important and will not be timed out. +printf"0\n"); the          Continue; -       } $        the        thell a0=A0; thell b0=B0; the  - Matrix tmp; inmemset (TMP.MP,0,sizeof(TMP.MP)); thetmp.mp[0][0]=1%MOD; thetmp.mp[1][0]=ax*by%MOD; Abouttmp.mp[1][1]=ax%MOD; thetmp.mp[1][3]=ax*by%MOD; thetmp.mp[2][0]=bx*ay%MOD; thetmp.mp[2][2]=bx%MOD; +tmp.mp[2][3]=bx*ay%MOD; -tmp.mp[3][0]=ax*bx%MOD; thetmp.mp[3][3]=ax*bx%MOD;Bayitmp.mp[4][0]=ay*by%MOD; thetmp.mp[4][1]=ay%MOD; thetmp.mp[4][2]=by%MOD; -tmp.mp[4][3]=ay*by%MOD; -tmp.mp[4][4]=1%MOD; the  theMatrix cnt=fastm (tmp,n-1); the  the Matrix G; -memset (G.MP,0,sizeof(G.MP)); theg.mp[0][0]=a0*b0%MOD; theg.mp[0][1]=a0%MOD; theg.mp[0][2]=b0%MOD;94g.mp[0][3]=a0*b0%MOD; theg.mp[0][4]=1%MOD; theMatrix ans=Mul (g,cnt); theprintf"%i64d\n", ans.mp[0][0]);98    } About     return 0; -}
View Code

Hdu 4686 ARC of Dream (Matrix fast power multiplication)

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.