拼凑硬币, 腾讯

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]
*/

上一篇:牛客多校第三场


下一篇:Codeforces Global Round 9 C. Element Extermination