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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值