結果

問題 No.3276 Make Smaller Popcount
ユーザー snrnsidy
提出日時 2025-09-19 21:37:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,919 bytes
コンパイル時間 1,792 ms
コンパイル使用メモリ 198,868 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-09-19 21:38:14
合計ジャッジ時間 42,088 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 25 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

long long int dp[32][32][2];

long long int solve(string s,int cnt)
{
    for(int i=0;i<=31;i++)
    {
        for(int a=0;a<=31;a++)
        {
            for(int j=0;j<2;j++)
            {
                dp[i][a][j] = 1e18;
            }
        }
    }

    dp[0][0][0] = 0;

    for(int i=0;i<31;i++)
    {
        for(int j=0;j<=i;j++)
        {
            for(int k=0;k<2;k++)
            {
                if(dp[i][j][k] >= 1e18) continue;
                int x = s[i] - '0';
                x += k;
                int c = x/2;
                if(x%2==0)
                {
                    dp[i+1][j][c] = min(dp[i+1][j][c],dp[i][j][k]);
                }
                else
                {
                    if(j+1 <= 31) dp[i+1][j+1][c] = min(dp[i+1][j+1][c],dp[i][j][k]);
                }

                x += 1;
                c = x/2;
                if(x%2==0)
                {
                    dp[i+1][j][c] = min(dp[i+1][j][c],dp[i][j][k] + (1LL << i));
                }
                else
                {
                    if(j+1 <= 31) dp[i+1][j+1][c] = min(dp[i+1][j+1][c],dp[i][j][k] + (1LL << i));
                }                
            }
        }
    }

    long long int res = 1e18;
    for(int i=0;i<cnt;i++)
    {
        res = min(res,dp[31][i][0]);
        res = min(res,dp[31][i][1]);
    }

    return res;
}
int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

    int tc;
    cin >> tc;

    while(tc--)
    {
        long long int n;
        cin >> n;

        int cnt = 0;
        string s = "";
        for(int x=0;x<31;x++)
        {
            int b = (n%2);
            cnt += b;
            s = s + (char)(b + '0');
            n/=2;
        }

        long long int value = solve(s,cnt);

        if(value >= 1e18) cout << -1 << '\n';
        else cout << value << '\n';

    }
	return 0;
}
0