結果
| 問題 |
No.1036 Make One With GCD 2
|
| コンテスト | |
| ユーザー |
rsk0315
|
| 提出日時 | 2020-04-25 03:00:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 305 ms / 2,000 ms |
| コード長 | 2,579 bytes |
| コンパイル時間 | 786 ms |
| コンパイル使用メモリ | 62,948 KB |
| 最終ジャッジ日時 | 2025-01-10 00:50:41 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
コンパイルメッセージ
test/yc_1036_test.cpp: In function ‘int main()’: test/yc_1036_test.cpp:42:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] test/yc_1036_test.cpp:45:26: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
ソースコード
#line 1 "test/yc_1036_test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1036"
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <numeric>
#include <vector>
#line 1 "DataStructure/foldable_queue.cpp"
/**
* @brief fold 可能キュー
* @author えびちゃん
*/
#include <cstddef>
#include <stack>
#include <utility>
template <class Monoid>
class foldable_queue {
public:
using size_type = size_t;
using value_type = Monoid;
private:
std::stack<value_type> M_front, M_back;
value_type M_back_folded{};
void M_distribute_to_front() {
if (!M_back.empty()) {
M_front.push(std::move(M_back.top()));
M_back.pop();
}
while (!M_back.empty()) {
M_back.top() += M_front.top();
M_front.push(std::move(M_back.top()));
M_back.pop();
}
M_back_folded = value_type{};
}
public:
size_type size() const { return M_front.size() + M_back.size(); }
bool empty() const noexcept { return M_front.empty() && M_back.empty(); }
void push(value_type const& x) {
M_back.push(x);
M_back_folded += M_back.top();
}
template <typename... Args>
void emplace(Args&&... args) {
M_back.emplace(std::forward<Args>(args)...);
M_back_folded += M_back.top();
}
void pop() {
if (M_front.empty()) M_distribute_to_front();
M_front.pop();
}
value_type fold() const {
if (M_front.empty()) return M_back_folded;
return M_front.top() + M_back_folded;
}
};
#line 10 "test/yc_1036_test.cpp"
template <typename Tp>
class gcd_monoid {
public:
using value_type = Tp;
private:
value_type M_x = 0;
public:
gcd_monoid() = default; // identity
gcd_monoid(value_type const& x): M_x(x) {}
gcd_monoid& operator +=(gcd_monoid const& that) {
M_x = std::gcd(M_x, that.M_x);
return *this;
}
friend bool operator ==(gcd_monoid const& lhs, gcd_monoid const& rhs) {
return lhs.M_x == rhs.M_x;
}
friend gcd_monoid operator +(gcd_monoid lhs, gcd_monoid const& rhs) { return lhs += rhs; }
friend bool operator !=(gcd_monoid const& lhs, gcd_monoid const& rhs) {
return !(lhs == rhs);
}
value_type const& get() const { return M_x; }
};
int main() {
size_t n;
scanf("%zu", &n);
std::vector<intmax_t> a(n);
for (auto& ai: a) scanf("%jd", &ai);
foldable_queue<gcd_monoid<intmax_t>> q;
intmax_t res = n*(n+1)/2;
for (size_t il = 0, ir = 0; il < n; ++il) {
while (ir < n && (q.fold() + a[ir]).get() != 1) {
q.push(a[ir++]);
}
res -= ir-il;
if (ir == il) ++ir; else q.pop();
}
printf("%jd\n", res);
}
rsk0315