HDU 4919 (数学推导)

题目链接:点击这里

1.当 n=2k+1 :

f(n)=i=1n1i(ni)=2i=1k2i(n2i)=2i=1k2i(2k2i+1)=4i=1ki(ki)+2k=4f(k)+6k

2.当 n=2k

f(n)=i=1n1i(ni)=i=1k2i(2k2i)+i=1k(2i1)(n2i+1)

对于左边那个式子:
i=1k2i(2k2i)=2i=1k1i(ki)=i=1k1i(ki)=2f(k)

对于右边那个式子:
i=1k(2i1)(n2i+1)=i=1k1(2i+1)(n2i1)=i=0k1(2i+1)[2(k1)2i+1]=2{i=0k1i(k1i)}=4k4+2[i=1k2i(k1i)]=4k4+2i=0k2i(k1i)=2f(k1)+4k4

加上去就是 f(n)=2f(k)+2f(k1)+4k4 .

import java.math.*;
import java.util.*;

public class Main {
    BigInteger x0 = BigInteger.valueOf (0);
    BigInteger x1 = BigInteger.valueOf (1);
    BigInteger x2 = BigInteger.valueOf (2);
    BigInteger x3 = BigInteger.valueOf (3);
    BigInteger x4 = BigInteger.valueOf (4);
    BigInteger x5 = BigInteger.valueOf (5);
    BigInteger x6 = BigInteger.valueOf (6);
    Map <BigInteger, BigInteger> gg = new HashMap <BigInteger, BigInteger> ();
    BigInteger dfs (BigInteger u) {
        if (gg.get (u) != null) {
            return gg.get (u);
        }
        BigInteger k = u.divide (x2);
        BigInteger mo = u.mod (x2);
        BigInteger ans;
        if (mo.compareTo (x1) == 0) {//奇数
            ans = dfs (k).multiply (x4).add (k.multiply (x6));
            gg.put (u, ans);
            return ans;
        }
        BigInteger kk = k.subtract (x1);
        ans = dfs (k).multiply (x2).add (dfs (kk).multiply (x2)).add (k.multiply (x4)).subtract (x4);
        gg.put (u, ans);
        return ans;
    }
    void solve () {
        Scanner cin = new Scanner (System.in);
        gg.put (x2, x0); gg.put (x3, x6); gg.put (x4, x4);
        while (cin.hasNext ()) {
            BigInteger n = cin.nextBigInteger ();
            n = dfs (n);
            System.out.println (n);
        }
    }
    public static void main (String[] args) {
        Main work = new Main();
        work.solve ();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值