プログラミングのお題スレ Part22 (831レス)
上下前次1-新
抽出解除 レス栞
812(5): 08/21(木)23:15 ID:0KQ1xtxb(1) AAS
>>799 の逆変換プログラム
R
外部リンク:ideone.com
C++
外部リンク:ideone.com
問題A, 問題Bとは違って、順列に出現するユニークな整数は1〜nの連番でなくても良いし、
出現回数はすべて同じでなくても良い。例えば、入力は [3, 1, 4, 1, 5, 9] でも良い
(ユニークな整数は1, 3, 4, 5, 9で、出現回数は1が2回、その他が1回)。
814(1): 08/22(金)06:55 ID:qlaiAqZd(1/2) AAS
>>812
Rust >>799の逆変換の重複順列の何番目か算出
use itertools::Itertools;
use num::{BigUint, One};
// 重複順列の何番目かを求める ex. "222331434114" → "123456"番目
fn to_nth(input: &str) -> String {
// 出現数
let (mut counts, chars): (Vec<usize Vec<char>) = input.chars().sorted().dedup_with_count().multiunzip();
// index化input
let input: Vec<usize> = input.chars().map(|c| chars.iter().position(|&c0| c0 == c).unwrap()).collect();
// 階乗関数
fn factorial(x: usize) -> BigUint { (1..=x).fold(BigUint::one(), |p, x| p * x) }
// 重複順列の総数
let mut whole: BigUint = factorial(input.len()) / counts.iter().map(|&x| factorial(x)).product::<BigUint>();
// nth番目を算出
(1..=input.len()).rev().zip(input).fold(BigUint::one(), |mut nth, (len, index)| {
// 自分より前までの総数をnthに足す
nth += &whole * counts[..index].iter().sum::<usize>() / len;
// 自分の総数へ更新
whole *= counts[index];
whole /= len;
counts[index] -= 1;
nth
})
.to_string()
}
820(1): 08/24(日)21:13 ID:ubCw2JoQ(1) AAS
>>812の逆変換プログラムは>>808の順変換プログラムを流用したから処理に無駄があった。
逆変換用に一から書き直したらすっきりした。
R
外部リンク:ideone.com
C++
外部リンク:ideone.com
827: 08/29(金)20:09 ID:VEuLqGzD(1) AAS
>>812 ruby 2.5.5
外部リンク:ideone.com
・tallyあるのは2.7以降
>>812 octave
外部リンク:ideone.com
830(1): 08/30(土)17:37 ID:zI+bKiSo(1) AAS
>>812 ocaml
外部リンク:ideone.com
>>812 scheme (chicken 4.13)
外部リンク:ideone.com
831: 830 09/02(火)21:36 ID:MM5Gazf9(1) AAS
>>812 scheme (chicken 4.13)
外部リンク:ideone.com
・集計部分をalistに変えてみただけ
上下前次1-新書関写板覧索設栞歴
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル
ぬこの手 ぬこTOP 0.033s