結果

問題 No.3266 岩井星人は見ずにはいられない
ユーザー elphe
提出日時 2025-08-10 10:27:56
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,106 bytes
コンパイル時間 3,097 ms
コンパイル使用メモリ 276,396 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-08-10 10:28:01
合計ジャッジ時間 4,687 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

static inline constexpr uint_fast64_t solve(const uint_fast32_t N, const uint_fast64_t A, const std::string& _S) noexcept
{
	uint_fast32_t loss = 0, i;
	uint_fast64_t AC_count = 0;
	for (i = 0; i != N; ++i)
	{
		if (AC_count == A)
			return i;
		else if (_S[i] == '0')
			++loss;
		else if (loss != 0)
			--loss, ++AC_count;
	}
	if (AC_count == A)
		return i;

	uint_fast32_t AC_count_per_cycle = 0;
	for (i = 0; i != N; ++i)
	{
		if (_S[i] == '0')
			++loss;
		else if (loss != 0)
			--loss, ++AC_count_per_cycle;
	}

	const uint_fast32_t count_cycle = (A - AC_count + AC_count_per_cycle - 1) / AC_count_per_cycle - 1;
	AC_count += static_cast<uint_fast64_t>(AC_count_per_cycle) * count_cycle;
	for (i = 0; AC_count != A; ++i)
	{
		if (_S[i] == '0')
			++loss;
		else if (loss != 0)
			--loss, ++AC_count;
	}

	return N * (count_cycle + 1) + i;
}

int main()
{
	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);

	uint_fast32_t N;
	uint_fast64_t A;
	std::string _S;
	std::cin >> N >> A;
	_S.reserve(N), std::cin >> _S;

	std::cout << solve(N, A, _S) << '\n';
	return 0;
}
0