zoukankan      html  css  js  c++  java
  • Saruman’s Level Up~(多校赛算组合数)

    Description

    Saruman’s army of orcs and other dark minions continuously mine and harvest lumber out of the land surrounding his mighty tower for N continuous days. On day number i, Saruman either chooses to spend resources on mining coal and harvesting more lumber, or on raising the level (i.e., height) of his tower. He levels up his tower by one unit only on days where the binary representation of i contains a total number of 1’s that is an exact multiple of 3. Assume that the initial level of his tower on day 0 is zero. For example, Saruman will level up his tower on day 7 (binary 111), next on day 11 (binary 1011) and then day 13, day 14, day 19, and so on. Saruman would like to forecast the level of his tower after N days. Can you write a program to help?

    Input

    The input file will contain multiple input test cases, each on a single line. Each test case consists of a positive integer N < 1016, as described above. The input ends on end of file.

    Output

    For each test case, output one line: “Day N: Level = L”, where N is the input N, and L is the number of levels after N days.

    Sample Input

    2
    19
    64
    

    Sample Output

    Day 2: Level = 0
    Day 19: Level = 5
    Day 64: Level = 21


    这题其实就是一个模拟,其实把n转化为二进制,
    然后求出小于等于n有多少个数的二进制中含有1的个数是3的倍数
    就是计算组合数,思维题,
    例如1000100 当计算到第一个1的时候 就是加上c(6,3)+c(6,6)
    此时bits的个数加一,然后到第二个1的时候就是看看
    (i+bits)%==0 0<=i<=2 时候存在 i 再加上组合数就是了

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 typedef long long LL;
     6 LL gcd(LL a, LL b ) {
     7     if (b == 0) return a;
     8     return gcd(b, a % b);
     9 }
    10 LL combine(LL n, LL r) {
    11     LL ret = 1;
    12     if (r > n - r) r = n - r;
    13     for (LL i = 1 ; i <= r ; i++) {
    14         LL k = n - i + 1;
    15         LL g = gcd(i, k);
    16         ret = ret / (i / g) * (k / g);
    17     }
    18     return ret;
    19 }
    20 LL ans(LL n) {
    21     LL ret = 0, bits = 0, k = ((LL)1 << 60), r = 60;
    22     while(k) {
    23         if (k & n) {
    24             for (int i = 0 ; i <= r ; i++) {
    25                 if ((i + bits) > 0 && ((i + bits) % 3 == 0)) ret += combine(r, i);
    26             }
    27             bits++;
    28         }
    29         k = k >> 1;
    30         r--;
    31     }
    32     if (bits > 0 && bits % 3 == 0) ret++;
    33     return ret;
    34 }
    35 int main() {
    36     LL n;
    37     while(scanf("%lld", &n) != EOF) {
    38         printf("Day %lld: Level = %lld
    ", n, ans(n));
    39     }
    40     return 0;
    41 }
    
    
    


  • 相关阅读:
    闭包_使用闭包
    闭包_理解闭包
    将视图直接转换成表的SQL语句
    基于先电的openstack云平台部署(IaaS 平台构建)
    Python替换掉列表的 和空格
    SQL语句生成一句话
    Clean-blog移植—博客园美化
    只是条咸鱼罢了
    基础平台-项目管理+组织管理心得
    关于springboot配置文件的一些心得
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/8877867.html
Copyright © 2011-2022 走看看