hdu 4919 数论+大数

来源:互联网 发布:报表数据分析 编辑:程序博客网 时间:2024/06/01 08:43

//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-4int 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;}


0 0