其他分享
首页 > 其他分享> > C. Factorials and Powers of Two(枚举,暴力)

C. Factorials and Powers of Two(枚举,暴力)

作者:互联网

C. Factorials and Powers of Two

Tag

枚举 暴力

题目来源

Codeforces Round #774 (Div. 2)

题目大意

解题思路

AC Code

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0)
#define LL long long
#define maxn (int)(2e6 + 10)
#define FFF freopen("out", "w", stdout);

LL n;
int a[maxn];
int ans[maxn];

LL fac[20];
void init()
{
    fac[0] = 1;
    for ( int i = 1 ; i <= 15 ; i++ )
        fac[i] = fac[i-1] * i;
    return ;
}

int check(LL x )
{
    int cnt = 0 ;
    while ( x)
    {
        if( x & 1 )
            cnt++; 
        x >>= 1;
    }
    return cnt;
}

int main ()
{
    IOS;
    init();
    int T ; cin >> T;
    for ( int cas = 1 ; cas <= T ; cas++ )
    {
        cin >> n;
        int minx = check(n);
        for ( int i = 8 ; i <= 1 << 15 ; i+=8 )
        {
            LL tmp = n ;
            int cnt = 0 ;
            for ( int j = 0 ; j <= 15 ; j++ )
                if ( (1 << j) & i ) 
                {
                    cnt++;
                    tmp -= fac[j];
                }
            if ( tmp < 0 ) break;
            minx = min(minx, cnt + check(tmp));
        }
        ans[cas] = minx;
    }
    for ( int i = 1 ; i <= T ; i++ )
        cout << ans[i] << endl;
}

标签:15,int,Two,maxn,阶乘,fac,Factorials,Powers,define
来源: https://www.cnblogs.com/I-wanna-be-the-kid/p/15975585.html