HDOJ 2256 Problem of Precision

Problem of Precision

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1325    Accepted Submission(s): 796


Problem Description

 

Input
The first line of input gives the number of cases, T. T test cases follow, each on a separate line. Each test case contains one positive integer n. (1 <= n <= 10^9)
 

Output
For each input case, you should output the answer in one line.
 

Sample Input
3 1 2 5
 

Sample Output
9 97 841
 

題意還是很簡單,想了半天也不知道怎麼玩,明顯最後的答案是類似:a+b*sqrt(6),既然這樣爲了不損失精度,我們保留sqrt(6)到最後,直接先求第n項的a,b


我弱雞的數學已經沒辦法了只有靠隊友了。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
int n,k;
int mod=1024;
struct Matrix
{
    int m[10][10];
}M;
Matrix Mult(Matrix a,Matrix b)
{
    Matrix ans;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            ans.m[i][j]=0;
            for(int k=0;k<n;k++)
            {
                ans.m[i][j]+=a.m[i][k]*b.m[k][j];
                ans.m[i][j]%=mod;
            }
        }
    }
    return ans;
}
Matrix quickpow(Matrix a,int b)
{
    Matrix ans;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i==j)
                ans.m[i][j]=1;
            else
                ans.m[i][j]=0;
        }
    }
    while(b)
    {
        if(b&1)
            ans=Mult(ans,a);
        a=Mult(a,a);
        b/=2;
    }
    return ans;
}
int a[10];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        Matrix M,N;
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
                M.m[i][j]=N.m[i][j]=0;
        }
        M.m[0][0]=5,M.m[0][1]=12;
        M.m[1][0]=2,M.m[1][1]=5;
        N.m[0][0]=5,N.m[1][0]=2;
        int m=n;
        n=2;
        M=quickpow(M,m-1);
        N=Mult(M,N);
        printf("%d\n",(2*N.m[0][0]-1)%mod);
    }
return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章