CSU2069: 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

Hint

Source

pacnw2012

题意:如果一个数n的二进制表示中的1的个数为3的倍数,那么这个数可以让高度加1。题目给定n,问从0到n一共有多少个数使得高度加1。

题解:首先我们求出给定数的二进制表示,对于该数字,我们可以将其分解成0 - 1111111...(比原先位数少1) 和 最高位10000000... -二进制表示数。对于0 - 1111111...这个范围,3的倍数的1的数量为C(n, 3) + C(n, 6) + ....C(n, x)(x <= n)。同理,对于最高位10000000... -二进制表示数,进行相同的操作分解。注意,因为此时最高位1一定存在,故之后应求解C(m, 2) + C(m, 5) + ...C(m, x)(x <= m)。同时我们注意到,每当3变成0时,当前的值也是一种解,故每次变成0答案加1。


AC代码

#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <sstream>
typedef long long ll;

using namespace std;

const int maxn = 100;
struct bign{
    int d[maxn], len;

    void clean() { while(len > 1 && !d[len-1]) len--; }

    bign()          { memset(d, 0, sizeof(d)); len = 1; }
    bign(int num)   { *this = num; }
    bign(char* num) { *this = num; }
    bign operator = (const char* num){
        memset(d, 0, sizeof(d)); len = strlen(num);
        for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
        clean();
        return *this;
    }
    bign operator = (int num){
        char s[20]; sprintf(s, "%d", num);
        *this = s;
        return *this;
    }

    bign operator + (const bign& b){
        bign c = *this; int i;
        for (i = 0; i < b.len; i++){
            c.d[i] += b.d[i];
            if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
        }
        while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
        c.len = max(len, b.len);
        if (c.d[i] && c.len <= i) c.len = i+1;
        return c;
    }
    bign operator - (const bign& b){
        bign c = *this; int i;
        for (i = 0; i < b.len; i++){
            c.d[i] -= b.d[i];
            if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
        }
        while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
        c.clean();
        return c;
    }
    bign operator * (const bign& b)const{
        int i, j; bign c; c.len = len + b.len;
        for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
            c.d[i+j] += d[i] * b.d[j];
        for(i = 0; i < c.len-1; i++)
            c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
        c.clean();
        return c;
    }
    bign operator / (const bign& b){
        int i, j;
        bign c = *this, a = 0;
        for (i = len - 1; i >= 0; i--)
        {
            a = a*10 + d[i];
            for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
            c.d[i] = j;
            a = a - b*j;
        }
        c.clean();
        return c;
    }
    bign operator % (const bign& b){
        int i, j;
        bign a = 0;
        for (i = len - 1; i >= 0; i--)
        {
            a = a*10 + d[i];
            for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
            a = a - b*j;
        }
        return a;
    }
    bign operator += (const bign& b){
        *this = *this + b;
        return *this;
    }

    bool operator <(const bign& b) const{
        if(len != b.len) return len < b.len;
        for(int i = len-1; i >= 0; i--)
            if(d[i] != b.d[i]) return d[i] < b.d[i];
        return false;
    }
    bool operator >(const bign& b) const{return b < *this;}
    bool operator<=(const bign& b) const{return !(b < *this);}
    bool operator>=(const bign& b) const{return !(*this < b);}
    bool operator!=(const bign& b) const{return b < *this || *this < b;}
    bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);}

    string str() const{
        char s[maxn]={};
        for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
        return s;
    }
};

bign pack[1000001];

istream& operator >> (istream& in, bign& x)
{
    string s;
    in >> s;
    x = s.c_str();
    return in;
}

ostream& operator << (ostream& out, const bign& x)
{
    out << x.str();
    return out;
}


ll num;
bign ans;

string a;
void get_two(ll n){
    string b, c;
    while(n){
        b += (n % 2) + '0';
        n /= 2;
    }
    for(ll i = 0; i < b.length(); i++)
        c += b[b.length() - 1 - i];
    a = c;
}

ll loc[111];

double lnchoose(ll n, ll m){
    if(m > n)
        return 0;
    if(m < n / 2.0)
        m = n - m;
    double s1 = 0;
    for(ll i = m + 1; i <= n; i++)
        s1 += log(double(i));
    double s2 = 0;
    ll ub = n - m;
    for(ll i = 2; i <= ub; i++)
        s2 +=log(double(i));
    return s1 - s2;
}

bign choose(ll n, ll m){
    if(m > n)
        return 0;
    bign ans(1);
    for (int i = 1; i <= min(m, n - m); i++)
        ans = ans * bign(n - i + 1);
    for (int i = 1; i <= min(m, n - m); i++)
        ans = ans / bign(i);
    return ans;
}

void solve(ll x, ll nn){
    x = a.length() - x - 1;
    while(x >= nn){
        bign k = choose(x,nn);
//        cout << k << " " << x << " " << nn << endl;
        ans += k;
        nn += 3;
    }
}

int main(){
    ll n;

    while(scanf("%lld", &n) != EOF){
        get_two(n);
        num = 1;
        for(ll i = 0; i < a.length(); i++)
            if(a[i] == '1')
                loc[num++] = i;
        ans = bign(0);
        ll nn = 3;
        for(ll i = 1; i < num; i++){
            solve(loc[i], nn);
            nn--;
            if(nn == 0)
                ans = ans + bign(1), nn += 3;
        }
        printf("Day %lld: Level = %s\n", n, ans.str().c_str());
    }
    return 0;
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页