HDU 2256 problem of precision (matrix fast power)

Source: Internet
Author: User

Address: HDU 2256

Ideas:

(SQRT (2) + SQRT (3) ^ 2 * n = (5 + 2 * SQRT (6) ^ N;

Note that (5 + 2 * SQRT (6) ^ N can be expressed as an + BN * SQRT (6 );

An + BN * (SQRT (6) = (5 + 2 * SQRT (6) * (A (n-1) + B (n-1) * SQRT (6 ))

= (5 * a (n-1) + 12 * B (n-1) + (2 * a (n-1) + 5 * B (n-1) * SQRT (6 );

Obviously, an = 5 * a (n-1) + 12 * B (n-1); bn = 2 * a (n-1) + 5 * B (n-1 );

At this time, it is easy to construct a matrix to quickly obtain an and BN:

5, 12

2, 5

What should we do next? The best way for me to wait is, of course .. Create a table .. Find the rule ..

Then the rule is ans = 2 * an-1;

How can we prove it? The proof is as follows:

(5 + 2 * SQRT (6) ^ n = An + BN * SQRT (6); (5-2 * SQRT (6 )) ^ n = An-BN * SQRT (6 );

(5 + 2 * SQRT (6) ^ N + (5-2 * SQRT (6) ^ n = 2 *;

Then, because

(5-2 * SQRT (6) ^ n = (0. 101...) ^ n <1;

Because

(5 + 2 * SQRT (6) ^ n = 2 * an-(5-2 * SQRT (6) ^ n

Available

2 * an-1 <(5 + 2 * SQRT (6) ^ n <2 *;

Therefore, the result of rounding (5 + 2 * SQRT (6) ^ n down must be 2 * an-1;

The proof is complete.

Therefore, you only need to use the Matrix to quickly obtain an power.

The Code is as follows:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;const int mod=1024;struct matrix{    int ma[3][3];}init, res;matrix Mult(matrix x, matrix y){    matrix tmp;    int i, j, k;    for(i=0;i<2;i++)    {        for(j=0;j<2;j++)        {            tmp.ma[i][j]=0;            for(k=0;k<2;k++)            {                tmp.ma[i][j]=(tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod;            }        }    }    return tmp;}matrix Pow(matrix x, int k){    int i, j;    matrix tmp;    for(i=0;i<2;i++) for(j=0;j<2;j++) tmp.ma[i][j]=(i==j);    while(k)    {        if(k&1) tmp=Mult(tmp,x);        x=Mult(x,x);        k>>=1;    }    return tmp;}int main(){    int t, k;    scanf("%d",&t);    while(t--)    {        scanf("%d",&k);        init.ma[0][0]=5;        init.ma[0][1]=12;        init.ma[1][0]=2;        init.ma[1][1]=5;        res=Pow(init,k-1);        int ans=(2*(res.ma[0][0]*5+res.ma[0][1]*2)-1)%mod;        printf("%d\n",ans);    }    return 0;}


HDU 2256 problem of precision (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.