hdu 4919 数论+大数

//f[n]=4f[k]+6k (n==2k+1)
//    =2f[k]+2[k-1]+4k-4  (n==2k)

要么、扩栈语句

G++:
int size = 128 << 20; // 128MB  如果题目64M,自然 64<<20
    char *p = (char*)malloc(size) + size;
    __asm__("movl %0, %%esp\n" :: "r"(p));

C++:
#pragma comment(linker, "/STACK:102400000,102400000")

要么、将递归转为非递归求解(标称这么做的)

//f[n]=4f[k]+6k (n==2k+1)
//    =2f[k]+2[k-1]+4k-4  (n==2k)
BigInteger contribution(const BigInteger &n, const BigInteger &coefficient) {
    BigInteger result;
    if (n[0] % 2) {
        result = (n / 2) * 6;
    } else {
        result = n * 2 - 4;
    }
    return result * coefficient;
}
//co[0] *f[x]+ co[1] *f[x+1]
//iff(x=2k)     =co[0]*f[2k]+co[1] *f[2k+1]=co[0]*(2f[k]+2f[k-1]+4*k-4)+co[1]*(4f[k]+6*k)
//              =2co[0]f[k-1]+(2co[0]+4co[1])f[k]+ co[0](4*k-4)+co[1]*6k;
//              x为偶数时,x传入时 为2*2k-4,  (x+1)传入时为(2k+1)/2*6

//iff(x=2k+1)   =co[0]*f[2k+1]+co[1] *f[2k+2]=co[0]*(4f[k]+6*k)+co[1]*(2f[k]+2f[k+1]+4*(k+1)-4)
//              =(4co[0]+2co[1])f[k]+2co[1]f[k+1]+ co[0](6*k)+co[1]*(4*(k+1)-4);
//              x为奇数时,x传入时 为(2k+1)/2*6,  (x+1)传入时为(2k+1+1)*2-4
int main()
{
    while (scanf("%s", buffer) == 1) {
        BigInteger n(buffer);
        BigInteger result = 0;
        std::vector <BigInteger> coefficient;
        coefficient.push_back(1);
        coefficient.push_back(0);
        while (n.length) {
            result = result + contribution(n, coefficient[0]) + contribution(n + 1, coefficient[1]);
            std::vector <BigInteger> new_coefficient;
            if (n[0] % 2) {
                n = n / 2;
                new_coefficient.push_back((coefficient[0] * 4) + (coefficient[1] * 2));
                new_coefficient.push_back((coefficient[1] * 2));
            } else {
                n = (n / 2) - 1;
                new_coefficient.push_back((coefficient[0] * 2));
                new_coefficient.push_back((coefficient[0] * 2) + (coefficient[1] * 4));
            }
            coefficient = new_coefficient;
        }
        result.print();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值