Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless committed Dec 19, 2021
1 parent 4b6fecc commit fcbb446
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
6 changes: 3 additions & 3 deletions 055.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
...
```

を利用することで、5 つの数の積が整数オーバーフローすることを回避する。(なお、`%` の優先度は `*` と等しい)
を利用することで、5 つの数の積が整数オーバーフローすることを回避します。ちなみに `%` の優先度は `*` と等しいです。

例:
```
Expand Down Expand Up @@ -79,9 +79,9 @@ int main()
```

## 解答 2 (定数倍高速化)
この問題における mod 演算は、[Barrett reduction](https://en.wikipedia.org/wiki/Barrett_reduction) というテクニックを使うことで、計算に時間がかかる `%` 演算子の使用を回避して定数倍高速化できる。AC Library の `atcoder::modint` ライブラリには Barrett reduction が実装されているため、それを利用するとコードも短くなり一石二鳥である
この問題における mod 演算は、[Barrett reduction](https://en.wikipedia.org/wiki/Barrett_reduction) というテクニックを使うことで、計算に時間がかかる `%` 演算子の使用を回避して定数倍高速化できます。AC Library の `atcoder::modint` ライブラリには Barrett reduction が実装されているため、それを利用するとコードも短くなり一石二鳥です

なお、`atcoder::modint` は、「C++ 標準の整数型 → `atcoder::modint`」の変換に通常の `%` 演算を使うため、プログラム全体を通してこの変換回数を減らすよう設計しないと速度向上効果が出ない(かえって遅くなることもある)。次のように、入力された値を最初にすべて `atcoder::modint` に変換して格納し、以降変換が発生しないようにする
なお、`atcoder::modint` は、「C++ 標準の整数型 → `atcoder::modint`」の変換に通常の `%` 演算を使うため、プログラム全体を通してこの変換回数を減らすよう設計しないと速度向上効果が出ません(かえって遅くなることもある)。次のように、入力された値を最初にすべて `atcoder::modint` に変換して格納し、以降で変換が発生しないようにします
```cpp
#include <iostream>
#include <vector>
Expand Down
32 changes: 32 additions & 0 deletions 061.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,37 @@
## 解答

```cpp
#include <iostream>
#include <deque>

int main()
{
// Q 回の操作
int Q;
std::cin >> Q;

// 山札(上から順に並んでいる)
std::deque<int> deck;

for (int i = 0; i < Q; ++i)
{
int t, x;
std::cin >> t >> x;

if (t == 1)
{
// 山札の一番上に追加
deck.push_front(x);
}
else if (t == 2)
{
// 山札の一番下に追加
deck.push_back(x);
}
else
{
std::cout << deck[x - 1] << '\n';
}
}
}
```
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# C++17 で解く「競プロ典型 90 問」

**「競プロ典型 90 問」** 、わかりやすい C++17 コードで解く、[@Reputeless](https://twitter.com/Reputeless) によるプロジェクトです。
**「競プロ典型 90 問」** Wを、わかりやすい C++17 コードで解く、[@Reputeless](https://twitter.com/Reputeless) によるプロジェクトです。

競技プログラミング固有のハックやスタイル(`<bits/stdc++.h>`, 大きな配列、マクロ、`using namespace std` 等)の使用を避けているため、一般的な C++ ソフトウェア開発でも使える、また C++ 標準ライブラリの機能 (`std::` から始まる) を意識したコーディングの練習ができます。

Expand Down Expand Up @@ -53,3 +53,4 @@
- [競プロ典型 90 問 - テストケース](https://www.dropbox.com/sh/nx3tnilzqz7df8a/AAC-L790bxKBVkmB6pdMUgk4a/typical90?dl=0&subfolder_nav_tracking=1)
- [AtCoder での実力アップを目指そう! ~競プロ典型 90 問~ - Qiita](https://qiita.com/e869120/items/1b2a5f0f07fd927e44e9)
- [「競プロ典型90問」非公式難易度表・ソースコード共有 - Google スプレッドシート](https://docs.google.com/spreadsheets/d/1GG4Higis4n4GJBViVltjcbuNfyr31PzUY_ZY1zh2GuI/edit#gid=0)
- [AC (AtCoder) Library Document](https://atcoder.github.io/ac-library/document_ja/index.html)

0 comments on commit fcbb446

Please sign in to comment.