2023-01-01から1ヶ月間の記事一覧

AtCoder Beginner Contest 287 D

https://atcoder.jp/contests/abc287/tasks/abc287_d前と後ろがどこまで合っているかを調べるだけですね。 zipが、 S.chars().zip(T.chars()) より zip(S.chars(), T.chars()) のほうが気持ちいいのですが、これも手元ではコンパイルできたのにAtCoderではエ…

アルゴリズムと数学 020

https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_t選んだ枚数とカードの数の和を状態としたDPですね。 // Choose Cards 1 #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mu</t:>…

アルゴリズムと数学 016

https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_pこの問題は、初期値を最初の項にするのでfoldは使えませんが、代わりにreduceが使えます。 A.into_iter().reduce(gcd).unwrap() 手元でコンパイルすると通りました。しかし、AtCo…

アルゴリズムと数学 013

https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_mナイーブに素因数分解して、あとは分割統治法的に約数を列挙します。 // Divisor Enumeration #![allow(non_snake_case)] //////////////////// library //////////////////// fn…

アルゴリズムと数学 012

https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_lミラー・ラビンを実装する余裕がないので、2から割っておきます。 // Primality Test #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() </t:>…

アルゴリズムと数学 007

https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_g包除原理ですね。これを使うために最小公倍数が要ります。 // Number of Multiples 1 #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() </t:>…

AtCoder Beginner Contest 286 D

https://atcoder.jp/contests/abc286/tasks/abc286_d和を状態としたDPですね。 // Money in Hand #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_lin</t:>…

アルゴリズムと数学 004

https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_d整数を3つではなく一般化しました。積にsumのようなものはないですが、foldが使えますね。 // Product of 3 Integers #![allow(non_snake_case)] //////////////////// library /…

アルゴリズムと数学 003

https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_cVecはIteratorにするとsumが使えます。 // Sum of N Integers #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = </t:>…

アルゴリズムと数学 001

https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_aこれから、アルゴリズムと数学 演習問題集を解いていきます。https://atcoder.jp/contests/math-and-algorithm // Print 5+N #![allow(non_snake_case)] //////////////////// li…

AtCoder Beginner Contest 285 D

https://atcoder.jp/contests/abc285/tasks/abc285_d(S, T)をエッジとしてグラフにします。それがループを含まないか調べます。少し前に連結成分に分けるコードを書いたので、連結成分に分けて、それぞれの成分が木になっているかを調べます。 // Change Use…

AtCoder Beginner Contest 265 D

https://atcoder.jp/contests/abc265/tasks/abc265_d各解はしゃくとり法で求められるので、その解をたどっていきます。 // Iroha and Haiku (New ABC Edition) #![allow(non_snake_case)] use std::collections::HashMap; //////////////////// library ////…

AtCoder Beginner Contest 266 D

https://atcoder.jp/contests/abc266/tasks/abc266_d各すぬけ君が現れる時刻に各穴での最大値を記憶していけばよいですね。 穴の数が固定なので、ここでは配列を使っています。 定数はこんな感じに定義します。 const M: usize = 5; 配列はこう初期化します…

AtCoder Beginner Contest 267 D

https://atcoder.jp/contests/abc267/tasks/abc267_d各Aに対して部分列の長さごとに最大値を求めればよいです。 // Index × A(Not Continuous ver.) #![allow(non_snake_case)] use std::cmp::{min, max}; //////////////////// library ///////////////////…

AtCoder Beginner Contest 284 D

https://atcoder.jp/contests/abc284/tasks/abc284_dまでにpかqのどちらかで割り切れます。 qで割り切れたら、を計算します。の計算はニュートン法的に、 , を収束するまで繰り返します。 // Happy New Year 2023 #![allow(non_snake_case)] ///////////////…

AtCoder Beginner Contest 268 D

https://atcoder.jp/contests/abc268/tasks/abc268_dSの間にアンダースコアを挟みますが、例えば、Sが4個で合わせて11文字だったら、最大5個のアンダースコアを3つに分配することになります。これは意外と簡単です。最初は、 1 1 1これに1ずつ加えていけばよ…

AtCoder Beginner Contest 269 D

https://atcoder.jp/contests/abc269/tasks/abc269_dグラフを連結成分に分けます。グラフの表現にはHashMapを使います。Vecでも表せるのですが、連結成分に分けるとなると効率が悪くなります。実際には分ける必要は無いのですが、後で使うために分けるコード…

AtCoder Beginner Contest 270 D

https://atcoder.jp/contests/abc270/tasks/abc270_d残りの石の数に対して、どちらも最善を尽くしたとき、先番がいくつ取れるかをDPにします。 ふつうにtake_whileが使えますね。 // Stones #![allow(non_snake_case)] //////////////////// library ///////…