結果
| 問題 |
No.1036 Make One With GCD 2
|
| コンテスト | |
| ユーザー |
chocorusk
|
| 提出日時 | 2020-03-15 01:21:19 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 385 ms / 2,000 ms |
| コード長 | 1,868 bytes |
| コンパイル時間 | 1,377 ms |
| コンパイル使用メモリ | 119,428 KB |
| 実行使用メモリ | 15,556 KB |
| 最終ジャッジ日時 | 2024-09-16 13:09:53 |
| 合計ジャッジ時間 | 10,793 ms |
|
ジャッジサーバーID (参考情報) |
judge6 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
ソースコード
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<typename T>
struct SWAG{
using F=function<T(T, T)>;
const F f;
stack<T> stl, str, str2;
SWAG(const F f): f(f){}
bool empty(){
if(stl.empty() && str.empty()) return true;
return false;
}
void push(const T &x){
if(str.empty()) str.push(x);
else str.push(f(str.top(), x));
str2.push(x);
}
void pop(){
assert(!empty());
if(stl.empty()){
while(!str2.empty()){
if(stl.empty()) stl.push(str2.top());
else stl.push(f(str2.top(), stl.top()));
str2.pop();
str.pop();
}
}
stl.pop();
}
T get(){
assert(!empty());
if(stl.empty()) return str.top();
else if(str.empty()) return stl.top();
else return f(stl.top(), str.top());
}
};
ll gcd(ll a, ll b){
if(b==0) return a;
return gcd(b, a%b);
}
int main()
{
int n;
scanf("%d", &n);
vector<ll> a(n);
for(int i=0; i<n; i++){
scanf("%lld", &a[i]);
}
SWAG<ll> seg([&](ll x, ll y){ return gcd(x, y);});
ll ans=0;
int r=-1;
for(int i=0; i<n; i++){
while(1){
if(!seg.empty() && seg.get()==1) break;
r++;
if(r<n) seg.push(a[r]);
else break;
}
if(r==n) break;
seg.pop();
ans+=n-r;
}
printf("%lld\n", ans);
return 0;
}
chocorusk