結果

問題 No.2939 Sigma Popcount Problem
ユーザー FplusFplusF
提出日時 2024-10-18 22:32:46
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,922 ms / 2,000 ms
コード長 1,453 bytes
コンパイル時間 3,309 ms
コンパイル使用メモリ 256,340 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-30 05:02:01
合計ジャッジ時間 17,733 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using ld=long double;
constexpr ll INF=(1ll<<60);
#define rep(i,n) for (ll i=0;i<(ll)(n);i++)
#define replr(i,l,r) for (ll i=(ll)(l);i<(ll)(r);i++)
#define all(v) v.begin(),v.end()
#define len(v) ((ll)v.size())
template<class T> inline bool chmin(T &a,T b){
    if(a>b){
        a=b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T &a,T b){
    if(a<b){
        a=b;
        return true;
    }
    return false;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll t;
    cin >> t;
    while(t--){
        ll n;
        cin >> n;
        vector<ll> s;
        while(0<n){
            s.push_back(n&1);
            n>>=1;
        }
        reverse(all(s));
        ll sz=len(s);
        vector<vector<vector<ll>>> dp(sz+1,vector<vector<ll>>(2,vector<ll>(sz+1,0)));
        dp[0][0][0]=1;
        rep(i,sz){
            rep(j,2){
                rep(k,j==0?s[i]+1:2){
                    rep(l,sz+1){
                        if(k==0) dp[i+1][j|(k<s[i])][l]+=dp[i][j][l];
                        if(l+1<=sz&&k==1) dp[i+1][j|(k<s[i])][l+1]+=dp[i][j][l];
                    }
                }
            }
        }
        ll ans=0;
        rep(i,sz+1){
            ans+=i*(dp[sz][0][i]+dp[sz][1][i]);
        }
        cout << ans << '\n';
    }
}
0