首页 > 其他 > 详细

拼凑硬币, 腾讯

时间:2020-07-11 14:15:35      阅读:85      评论:0      收藏:0      [点我收藏+]
import java.util.*;
public class Main {
    static Map<Long, Long> map;
    static long solve(long n){
        if(map.containsKey(n)) return map.get(n);
        long t = solve(n>>1);
        map.put(n>>1, t);
        if(n % 2 == 0) {
            long t2 = solve((n>>1)-1);
            map.put((n>>1)-1, t2);
            t += t2;
        }
        return t;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        map = new HashMap<>();
        map.put(0L, 1L);
        map.put(1L, 1L);
        long n = sc.nextLong();
        System.out.println(solve(n));
    }
}

/*
n为偶数时,使用2个1块钱,或者不使用1块钱
f[n] = f[(n>>1) - 1] + f[n>>1] 
n为奇数时, 必须使用一个一个块钱
f[n] = f[n>>1]
*/

拼凑硬币, 腾讯

原文:https://www.cnblogs.com/lixyuan/p/13283191.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!