結果
| 問題 |
No.1036 Make One With GCD 2
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-25 20:22:21 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 370 ms / 2,000 ms |
| コード長 | 2,817 bytes |
| コンパイル時間 | 1,911 ms |
| コンパイル使用メモリ | 134,392 KB |
| 最終ジャッジ日時 | 2025-01-10 01:29:08 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:108:16: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
108 | ll N; scanf("%lld", &N);
| ~~~~~^~~~~~~~~~~~
main.cpp:109:55: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
109 | vector<ll> a(N); for (int i = 0; i < N; i++) scanf("%lld", &a[i]);
| ~~~~~^~~~~~~~~~~~~~~
ソースコード
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
template< typename SemiGroup >
struct SlidingWindowAggregation {
using F = function< SemiGroup(SemiGroup, SemiGroup) >;
struct Node {
SemiGroup val, sum;
Node(const SemiGroup& val, const SemiGroup& sum) : val(val), sum(sum) {}
};
SlidingWindowAggregation(F f) : f(f) {}
const F f;
stack< Node > front, back;
bool empty() const {
return front.empty() && back.empty();
}
size_t size() const {
return front.size() + back.size();
}
SemiGroup fold_all() const {
if (front.empty()) {
return back.top().sum;
}
else if (back.empty()) {
return front.top().sum;
}
else {
return f(front.top().sum, back.top().sum);
}
}
void push(const SemiGroup& x) {
if (back.empty()) {
back.emplace(x, x);
}
else {
back.emplace(x, f(back.top().sum, x));
}
}
void pop() {
if (front.empty()) {
front.emplace(back.top().val, back.top().val);
back.pop();
while (!back.empty()) {
front.emplace(back.top().val, f(back.top().val, front.top().sum));
back.pop();
}
}
front.pop();
}
};
ll f(ll a, ll b) {
return gcd(a, b);
}
int main()
{
/*
cin.tie(nullptr);
ios::sync_with_stdio(false);
*/
ll N; scanf("%lld", &N);
vector<ll> a(N); for (int i = 0; i < N; i++) scanf("%lld", &a[i]);
SlidingWindowAggregation<ll> swag(f);
ll right = 0;
ll res = 0;
for (int left = 0; left < N; left++) {
if (left != 0) swag.pop();
while (right != N and (swag.empty() or swag.fold_all() != 1)) {
swag.push(a[right]);
right++;
}
if (swag.fold_all() == 1) res += N - right + 1;
}
cout << res << endl;
return 0;
}