提出 #65924905


ソースコード 拡げる

#include <iostream>
using namespace std;
#define LL long long
const LL M = 998244353;

LL n, k, d[65], ans;
LL f[65][2][65]; // f[i][j][k],长度为i,以j开头,包含k个1,数字的个数
LL g[65][2][65]; // 数字之和

LL er(LL x) {
	return (1 << x) % M;
}

LL dg(LL i, LL k) {
	if (k < 0) return 0;
	if (i == 0) {
		return (k==0);
	}
	
	if (d[i] == 1) {
		ans = (ans + g[i][0][k]) % M;
		// cout << "add g" << i << ' ' << 0 << ' ' << k << "=" << g[i][0][k] << endl;
		LL num = dg(i-1, k-1);
		ans = (ans + num * er(i-1)) % M;
		// cout << "add " << num << "*" << (1<<(i-1)) << endl;
		return (num + f[i][0][k]) % M;
	}
	else {
		return dg(i-1, k);
	}
}

void work() {
	LL cnt = 0;
	ans = 0;
	while (n > 0) {
		d[++cnt] = n % 2;
		n /= 2;
	}
	dg(cnt, k);
	cout << ans << endl;
}

void Init() {
	f[1][0][0] = 1;
	f[1][1][1] = 1;
	g[1][1][1] = 1;
	for(int i = 2; i <= 60; i++) {
		for(int j = 0; j <= 1; j++) {
			for(int k = 0; k <= i; k++) {
				if (k-j >= 0) {
					f[i][j][k] = f[i-1][0][k-j] + f[i-1][1][k-j];
					g[i][j][k] = (g[i-1][0][k-j] + g[i-1][1][k-j]) % M;
					if (j == 1) g[i][j][k] = (g[i][j][k] + f[i][j][k] * er(i-1)) % M;
				}
			}
		}
	}
}

LL popcnt(LL x) {
	int num = 0;
	while (x > 0) {
		num += (x % 2);
		x /= 2;
	}
	return num;
}

void bl() {
	LL ans = 0;
	for(int i = 1; i <= n; i++) {
		if (popcnt(i) == k) ans += i;
	}
	cout << ans << endl;
}

int main()
{
	Init();
	int T;
	cin >> T;
	while (T--) {
		cin >> n >> k; // 1~N 内, popcount(i) = k 的 i的和
		// bl();
		work();
	}
	return 0;
}

提出情報

提出日時
問題 E - Popcount Sum 3
ユーザ gobywind
言語 C++ 20 (gcc 12.2)
得点 0
コード長 1625 Byte
結果 WA
実行時間 1 ms
メモリ 3820 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 450
結果
AC × 1
AC × 3
WA × 31
セット名 テストケース
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
ケース名 結果 実行時間 メモリ
example_00.txt AC 1 ms 3816 KiB
hand_00.txt WA 1 ms 3700 KiB
hand_01.txt AC 1 ms 3704 KiB
hand_02.txt AC 1 ms 3752 KiB
random_00.txt WA 1 ms 3624 KiB
random_01.txt WA 1 ms 3652 KiB
random_02.txt WA 1 ms 3656 KiB
random_03.txt WA 1 ms 3696 KiB
random_04.txt WA 1 ms 3696 KiB
random_05.txt WA 1 ms 3592 KiB
random_06.txt WA 1 ms 3608 KiB
random_07.txt WA 1 ms 3636 KiB
random_08.txt WA 1 ms 3592 KiB
random_09.txt WA 1 ms 3568 KiB
random_10.txt WA 1 ms 3612 KiB
random_11.txt WA 1 ms 3652 KiB
random_12.txt WA 1 ms 3700 KiB
random_13.txt WA 1 ms 3592 KiB
random_14.txt WA 1 ms 3592 KiB
random_15.txt WA 1 ms 3676 KiB
random_16.txt WA 1 ms 3592 KiB
random_17.txt WA 1 ms 3820 KiB
random_18.txt WA 1 ms 3812 KiB
random_19.txt WA 1 ms 3624 KiB
random_20.txt WA 1 ms 3704 KiB
random_21.txt WA 1 ms 3624 KiB
random_22.txt WA 1 ms 3576 KiB
random_23.txt WA 1 ms 3704 KiB
random_24.txt WA 1 ms 3624 KiB
random_25.txt WA 1 ms 3616 KiB
random_26.txt WA 1 ms 3568 KiB
random_27.txt WA 1 ms 3752 KiB
random_28.txt WA 1 ms 3776 KiB
random_29.txt WA 1 ms 3676 KiB