HDU-2256

    转载于:http://www.cnblogs.com/staginner/archive/2012/04/24/2468380.html

 
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

    题意:问你(根号2+根号3)的2n次方mod1024的结果。

    这个题目用快速幂+fmod是AC不了的,应该是浮点数精度的原因,推导的过程都体现在图上了,剩下的工作就是二分矩阵求解了。

    此外,在计算完x[n]和y[n]之后不能直接用(x[n]+(int)(y[n]*sqrt(6.0)))%1024来得到最后的结果的,先取整再模和先模再取整的结果是不一样的这一点举个例子就比较容易看出来了。

    比如(2000*1.372)%1000,这样先乘再取模得到的结果就是744,如果先把2000模1000,显然最后结果就是0了。

    

 

复制代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAXD 2
int N, cnt;
struct Matrix
{
    int a[MAXD][MAXD];
    void init()
    {
        a[0][0] = a[1][1] = 5, a[0][1] = 12, a[1][0] = 2;
    }
}mat[150];
int multiply(int x, int y)
{
    int i, j, k, z = ++ cnt, ans;
    for(i = 0; i < 2; i ++)
        for(j = 0; j < 2; j ++)
        {
            ans = 0;
            for(k = 0; k < 2; k ++)
                ans += mat[x].a[i][k] * mat[y].a[k][j];
            mat[z].a[i][j] = ans & 1023;
        }
    return z;
}
int powmod(int n)
{
    int k;
    if(n == 1)
        return 0;
    k = powmod(n >> 1);
    k = multiply(k, k);
    if(n & 1)
        k = multiply(k, 0);
    return k;
}
void solve()
{
    int k, ans, x, y;
    cnt = 0;
    mat[0].init();
    k = powmod(N);
    ans = (2 * mat[k].a[0][0] - 1 + 1024) & 1023;
    printf("%d\n", ans);
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t --)
    {
        scanf("%d", &N);
        solve();
    }
    return 0;
}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值