高校数学の質問スレ(医者・東大卒専用) Part438 (906レス)
高校数学の質問スレ(医者・東大卒専用) Part438 http://rio2016.5ch.net/test/read.cgi/math/1723152147/
上
下
前
次
1-
新
通常表示
512バイト分割
レス栞
756: 132人目の素数さん [sage] 2025/03/24(月) 08:15:20.18 ID:Wji9obyD 赤玉5個、黒玉3個、白玉2個を(4個, 3個, 3個)に分ける方法(もらう人を区別しない)」を全て列挙せよ library(gtools) # permutations を使用 # 各色の玉の個数 balls <- c(赤 = 4, 黒 = 3, 白 = 2, 青 = 1) # 各箱のサイズ box_sizes <- c(4, 3, 3) # 指定したサイズで玉を選ぶ関数 generate_combinations <- function(balls, size) { colors <- names(balls) all_combinations <- permutations(n = length(colors), r = size, v = colors, repeats.allowed = TRUE) # 各組み合わせが元の玉の数を超えないようにフィルタリング valid_combinations <- all_combinations[apply(all_combinations, 1, function(combo) { all(table(combo) <= balls[names(table(combo))]) }), ] return(unique(t(apply(valid_combinations, 1, sort)))) # 各組み合わせをソートして重複を削除 } # すべての分割を求める unique_distributions <- list() first_box_choices <- generate_combinations(balls, box_sizes[1]) for (first_box in 1:nrow(first_box_choices)) { remaining1 <- balls first_selection <- first_box_choices[first_box, ] for (color in first_selection) { remaining1[color] <- remaining1[color] - 1 } second_box_choices <- generate_combinations(remaining1, box_sizes[2]) for (second_box in 1:nrow(second_box_choices)) { remaining2 <- remaining1 second_selection <- second_box_choices[second_box, ] for (color in second_selection) { remaining2[color] <- remaining2[color] - 1 } # 第3箱は残りの玉で確定 third_box <- rep(names(remaining2), times = remaining2) # 各箱内をソートし、3つの箱の並びも統一 sorted_distribution <- list(sort(first_selection), sort(second_selection), sort(third_box)) sorted_distribution <- sorted_distribution[order(sapply(sorted_distribution, paste, collapse = ""))] # 箱順も統一 # **文字列化してリストを扱いやすくする** unique_distributions <- append(unique_distributions, list(paste(sapply(sorted_distribution, paste, collapse = ","), collapse = " | "))) } } # 重複を削除 unique_distributions <- unique(unique_distributions) # 総数を確認(正しく63になるはず) length(unique_distributions) # 結果を表示 print(unique_distributions) http://rio2016.5ch.net/test/read.cgi/math/1723152147/756
メモ帳
(0/65535文字)
上
下
前
次
1-
新
書
関
写
板
覧
索
設
栞
歴
あと 150 レスあります
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
0.021s