結果

問題 No.1036 Make One With GCD 2
ユーザー tko919tko919
提出日時 2020-04-25 20:20:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 487 ms / 2,000 ms
コード長 1,848 bytes
コンパイル時間 1,794 ms
コンパイル使用メモリ 174,204 KB
実行使用メモリ 23,076 KB
最終ジャッジ日時 2023-10-14 20:02:14
合計ジャッジ時間 13,631 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
8,224 KB
testcase_01 AC 104 ms
6,904 KB
testcase_02 AC 131 ms
23,076 KB
testcase_03 AC 19 ms
4,380 KB
testcase_04 AC 32 ms
5,668 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 36 ms
4,724 KB
testcase_08 AC 30 ms
4,508 KB
testcase_09 AC 285 ms
7,188 KB
testcase_10 AC 265 ms
6,748 KB
testcase_11 AC 290 ms
6,972 KB
testcase_12 AC 267 ms
6,776 KB
testcase_13 AC 482 ms
6,708 KB
testcase_14 AC 487 ms
7,020 KB
testcase_15 AC 456 ms
6,508 KB
testcase_16 AC 460 ms
6,948 KB
testcase_17 AC 471 ms
6,724 KB
testcase_18 AC 1 ms
4,368 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 3 ms
4,368 KB
testcase_21 AC 3 ms
4,372 KB
testcase_22 AC 453 ms
6,488 KB
testcase_23 AC 332 ms
5,764 KB
testcase_24 AC 469 ms
6,760 KB
testcase_25 AC 428 ms
6,548 KB
testcase_26 AC 444 ms
6,512 KB
testcase_27 AC 1 ms
4,372 KB
testcase_28 AC 1 ms
4,372 KB
testcase_29 AC 1 ms
4,368 KB
testcase_30 AC 1 ms
4,368 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 1 ms
4,368 KB
testcase_33 AC 1 ms
4,368 KB
testcase_34 AC 1 ms
4,372 KB
testcase_35 AC 2 ms
4,368 KB
testcase_36 AC 1 ms
4,372 KB
testcase_37 AC 2 ms
4,372 KB
testcase_38 AC 132 ms
23,032 KB
testcase_39 AC 124 ms
7,076 KB
testcase_40 AC 332 ms
5,652 KB
testcase_41 AC 456 ms
7,336 KB
testcase_42 AC 456 ms
7,220 KB
testcase_43 AC 391 ms
10,104 KB
testcase_44 AC 433 ms
8,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0