EGEsoll - сборник решений задач из ЕГЭ

Задача 2

Борис составляет 6-⁠буквенные коды из букв Б, О, Р, И, С. Буквы Б и Р нужно обязательно использовать ровно по одному разу, букву С можно использовать один раз или не использовать совсем, буквы О и И можно использовать произвольное количество раз или не использовать совсем. Сколько различных кодов может составить Борис?

Добавлено: 29.03.26 11:59

Перейти к решению

Решение

Приведём решение на Python:

from itertools import product

k = 0
for p in product("БОРИС", repeat=6):
  if p.count("Б") == 1 and p.count("Р") == 1 and p.count("С") <= 1:
    k += 1
    print("".join(p), "".join(p).count("C"))
print(k) # 1440

Будьте внимательны! Можно перепутать кирилическую букву C и латинскую! В таком случае код будет неисправен.

Ответ: 1440

ПЕРВОАПРЕЛЬСКОЕ ОБНОВЛЕНИЕ!

Решение на C++:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

void generate_product(const std::vector<char>& alphabet,
                      int length,
                      std::string& current,
                      std::vector<std::string>& results) {
  if (static_cast<int>(current.size()) == length) {
    results.push_back(current);
    return;
  }
  for (char c : alphabet) {
    current.push_back(c);
    generate_product(alphabet, length, current, results);
    current.pop_back();
  }
}

int main() {
  const std::vector<char> alphabet = {'B', 'O', 'R', 'I', 'S'};
  const int repeat = 6;
  std::vector<std::string> combos;
  std::string buffer;

  generate_product(alphabet, repeat, buffer, combos);

  int k = 0;

  for (const std::string& s : combos) {
    int countB = std::count(s.begin(), s.end(), 'B');
    int countR = std::count(s.begin(), s.end(), 'R');
    int countS = std::count(s.begin(), s.end(), 'S');

    if (countB == 1 && countR == 1 && countS <= 1) {
      ++k;
    }
  }

  std::cout << k << std::endl; // 1440
  return 0;
}

Автор - rubygem17

Объяснение

None

Назад