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): 1057    Accepted Submission(s): 624


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
 

Source


解法参考 http://blog.youkuaiyun.com/qq_15714857/article/details/47705581

代码如下:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define mod  1024
struct Node{
    int a[2][2];
    Node(){
        memset(a,0,sizeof(a));
    }
    Node operator * (const Node & b){
        Node c;
        memset(c.a,0,sizeof(c.a));
        for(int i = 0;i < 2; i++)
            for(int j = 0;j < 2; j++)
                for(int k = 0;k < 2; k++)
                    c.a[i][j] = (c.a[i][j]+a[i][k]*b.a[k][j])%mod;
        return c;
    }
};






int main(){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        Node a,b,c;
        a.a[0][0] = 5;
        a.a[0][1] = 12;
        a.a[1][0] = 2;
        a.a[1][1] = 5;
        b.a[0][0] = 1;
        while(n){
            if(n&1)b = b*a;
            a = a*a;
            n/=2;
        }
        int ans = b.a[0][0]*2-1;
        printf("%d\n",ans%mod);

    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值