結果
| 問題 |
No.1036 Make One With GCD 2
|
| コンテスト | |
| ユーザー |
tko919
|
| 提出日時 | 2020-04-25 20:20:23 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 479 ms / 2,000 ms |
| コード長 | 1,848 bytes |
| コンパイル時間 | 1,808 ms |
| コンパイル使用メモリ | 176,884 KB |
| 実行使用メモリ | 23,388 KB |
| 最終ジャッジ日時 | 2024-09-16 13:55:32 |
| 合計ジャッジ時間 | 13,272 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
ソースコード
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>(b);i--)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
void tostr(ll x,string& res){while(x)res+=('0'+(x%10)),x/=10; reverse(ALL(res)); return;}
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; }
//template end
template<class M>struct SWAG{
using F=function<M(M,M)>; F f; M m0;
stack<M> fval,bval; vector<M> fsum,bsum;
SWAG(const F _f,M _m0):f(_f),m0(_m0){}
M fold(){
if(fsum.empty()){
if(bsum.empty())return m0;
return bsum.back();
}else{
if(bsum.empty())return fsum.back();
else return f(fsum.back(),bsum.back());
}
}
void push(M x){
bval.push(x);
if(bsum.empty())bsum.push_back(x);
else{
M y=f(bsum.back(),x);
bsum.push_back(y);
}
}
void pop(){
if(fval.empty()){
bsum.clear();
while(!bval.empty()){
auto x=bval.top(); bval.pop();
fval.push(x);
if(fsum.empty())fsum.push_back(x);
else{
M y=f(x,fsum.back());
fsum.push_back(y);
}
}
}
fval.pop(); fsum.pop_back();
}
};
int main(){
int n; scanf("%d",&n);
vector<ll> a(n); rep(i,0,n)scanf("%lld",&a[i]);
SWAG<ll> swag([](ll a,ll b){return __gcd(a,b);},0);
ll rb=0,res=0;
rep(i,0,n){
if(i)swag.pop();
while(rb<n and swag.fold()!=1){swag.push(a[rb]); rb++;}
if(swag.fold()==1)res+=n-rb+1;
} printf("%lld\n",res);
return 0;
}
tko919