HDU_2256_Problem of Precision

Problem of Precision

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


归纳推理+矩阵快速幂;
Problem Description

 

推理过程(引用大牛的):

1 题目要求的是(sqrt(2)+sqrt(3))^2n %1024向下取整的值

  矩阵快速幂专题【陆续更新中】

  矩阵快速幂专题【陆续更新中】

3 这里很多人会直接认为结果等于(an+bn*sqrt(6))%1024,但是这种结果是错的,因为这边涉及到了double,必然会有误差,所以根double有关的取模都是错误的思路

 



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
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3117  2254  1588  2294  2276 
 

Statistic | Submit | Discuss | Note


#include <iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<cstring>
using namespace std;
const int N=1e6+7;
struct Matrix
{
    int x,y;
    int A[10][10];
    void Clean()
    {
        x=y=0;
        memset(A,0,sizeof(A));
    }
    Matrix operator *(const Matrix& B)const
    {
        Matrix tmp;
        tmp.Clean();
        tmp.x=x,tmp.y=B.y;
        for(int i=0;i<x;i++)
        {
            for(int j=0;j<B.y;j++)
            {
                for(int k=0;k<x;k++)
                {
                    (tmp.A[i][j]+=A[i][k]*B.A[k][j])%=1024;
                }
            }
        }
        return tmp;
    }
}S,ans;
int power(int n)
{
    ans.Clean();
    ans.x=2;
    ans.y=1;
    ans.A[0][0]=5;
    ans.A[1][0]=2;
    S.Clean();
    S.x=S.y=2;
    S.A[0][0]=5;
     S.A[0][1]=12;
     S.A[1][0]=2;
     S.A[1][1]=5;
     while(n)
     {
         if(n&1)
            ans=S*ans;
         S=S*S;
         n>>=1;
     }
return (ans.A[0][0]*2-1)%1024;
}
int main()
{
    int n;
    int t;
    cin>>t;
    while(t--)
    {cin>>n;
        printf("%d\n",power(n-1));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值