結果
| 問題 |
No.3135 AAABC
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-05-23 12:31:40 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,059 bytes |
| コンパイル時間 | 3,389 ms |
| コンパイル使用メモリ | 277,624 KB |
| 実行使用メモリ | 7,844 KB |
| 最終ジャッジ日時 | 2025-05-23 12:31:45 |
| 合計ジャッジ時間 | 4,771 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 32 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
long long S;
cin >> N >> S;
// Precompute combinations C[n][k] for n,k up to 3
long long comb[4][4] = {
{1,0,0,0},
{1,1,0,0},
{1,2,1,0},
{1,3,3,1}
};
// Precompute powers powTable[b][e] = b^e for b=0..3, e=0..12
const int MAXN = 12;
static long long powTable[4][MAXN+1];
for (int b = 0; b <= 3; ++b) {
powTable[b][0] = 1;
for (int e = 1; e <= MAXN; ++e) {
powTable[b][e] = powTable[b][e-1] * b;
}
}
// Total number of valid strings of length N using A,B,C each at least once
long long total = 0;
for (int j = 0; j <= 3; ++j) {
long long sign = (j % 2 ? -1 : 1);
total += sign * comb[3][j] * powTable[3 - j][N];
}
if (S > total) {
cout << "-1\n";
return 0;
}
string ans;
ans.reserve(N);
int countArr[3] = {0, 0, 0}; // counts for A, B, C
for (int i = 0; i < N; ++i) {
int rem = N - i - 1;
for (int idx = 0; idx < 3; ++idx) {
// try placing char = 'A'+idx
int newCount[3] = { countArr[0], countArr[1], countArr[2] };
newCount[idx]++;
// how many of A,B,C still missing?
int need = 0;
for (int k = 0; k < 3; ++k) {
if (newCount[k] == 0) ++need;
}
// count ways to fill rem positions so as to include all needed letters
long long cnt = 0;
for (int j = 0; j <= need; ++j) {
long long sign = (j % 2 ? -1 : 1);
cnt += sign * comb[need][j] * powTable[3 - j][rem];
}
if (cnt >= S) {
// choose this character
ans.push_back(char('A' + idx));
countArr[idx]++;
break;
} else {
S -= cnt;
}
}
}
cout << ans << "\n";
return 0;
}