結果
| 問題 |
No.1036 Make One With GCD 2
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-26 01:39:33 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,526 bytes |
| コンパイル時間 | 1,995 ms |
| コンパイル使用メモリ | 176,208 KB |
| 実行使用メモリ | 15,616 KB |
| 最終ジャッジ日時 | 2024-11-08 14:38:57 |
| 合計ジャッジ時間 | 12,925 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 2 |
| other | AC * 6 WA * 35 |
コンパイルメッセージ
main.cpp: In member function 'void Swag<Operator>::pop()':
main.cpp:72:38: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
72 | auto [val,_] = suf.top();
| ^
main.cpp: At global scope:
main.cpp:86:9: warning: inline variables are only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
86 | inline static constexpr TypeNode unit_node = 0;
| ^~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define SPEED cin.tie(0);ios::sync_with_stdio(false);
class Gcd{
public:
inline static long long impl(long long n, long long m) {
static constexpr long long K = 5;
long long t,s;
for(int i = 0; t = n - m, s = n - m * K, i < 80; ++i) {
if(t<m){
if(!t) return m;
n = m, m = t;
}
else{
if(!m) return t;
n=t;
if(t >= m * K) n = s;
}
}
return impl(m, n % m);
}
inline static long long pre(long long n, long long m) {
long long t;
for(int i = 0; t = n - m, i < 4; ++i) {
(t < m ? n=m,m=t : n=t);
if(!m) return n;
}
return impl(n, m);
}
inline static long long gcd(long long n, long long m) {
return (n>m ? pre(n,m) : pre(m,n));
}
inline static constexpr long long pureGcd(long long a, long long b) {
return (b ? pureGcd(b, a % b):a);
}
inline static constexpr long long lcm(long long a, long long b) {
return (a*b ? (a / gcd(a, b)*b): 0);
}
inline static constexpr long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) return x = 1, y = 0, a;
long long d = extGCD(b, a%b, y, x);
return y -= a / b * x, d;
}
};
template<class Operator> class Swag{
public:
using TypeNode = typename Operator::TypeNode;
stack<pair<TypeNode,TypeNode>> pre,suf;
Swag() {
// do nothing
}
TypeNode fold() {
TypeNode res = Operator::unit_node;
if(pre.size()) res = Operator::funcNode(res,pre.top().second);
if(suf.size()) res = Operator::funcNode(res,suf.top().second);
return res;
}
void push(TypeNode val) {
TypeNode acc = val;
if(suf.size()) acc = Operator::funcNode(acc,suf.top().second);
suf.emplace(val,acc);
}
void pop() {
if(pre.empty()) {
TypeNode acc = Operator::unit_node;
while(suf.size()) {
auto [val,_] = suf.top();
suf.pop();
acc = Operator::funcNode(acc,val);
pre.emplace(val,acc);
}
}
else{
pre.pop();
}
}
};
template<class T> struct nodeGCD {
using TypeNode = T;
inline static constexpr TypeNode unit_node = 0;
inline static constexpr TypeNode funcNode(TypeNode l,TypeNode r){return Gcd::gcd(l,r);}
};
// solution by binary search in arbitary range on disjn sparse table
int main() {
SPEED
ll N; cin >> N;
vector<ll> A(N+1,1);
for(int i = 0; i < N; ++i) cin >> A[i];
Swag<nodeGCD<ll>> swag;
ll ans=0;
int r=1;
for(int i=0; i<N; i++,r=max(r,i+1)){
while(r<=N){
if(swag.fold()==1) break;
swag.push(A[r-1]);
r++;
}
ans+=N+1-r;
swag.pop();
}
cout << ans << endl;
}