結果

問題 No.1036 Make One With GCD 2
ユーザー chocoruskchocorusk
提出日時 2020-03-15 01:21:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 389 ms / 2,000 ms
コード長 1,868 bytes
コンパイル時間 2,737 ms
コンパイル使用メモリ 116,252 KB
実行使用メモリ 15,264 KB
最終ジャッジ日時 2023-10-14 19:11:27
合計ジャッジ時間 12,406 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
7,508 KB
testcase_01 AC 85 ms
6,916 KB
testcase_02 AC 109 ms
15,256 KB
testcase_03 AC 16 ms
4,352 KB
testcase_04 AC 26 ms
5,660 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 33 ms
4,608 KB
testcase_08 AC 26 ms
4,392 KB
testcase_09 AC 231 ms
6,876 KB
testcase_10 AC 212 ms
6,944 KB
testcase_11 AC 227 ms
7,040 KB
testcase_12 AC 217 ms
6,780 KB
testcase_13 AC 383 ms
6,716 KB
testcase_14 AC 389 ms
6,980 KB
testcase_15 AC 361 ms
6,588 KB
testcase_16 AC 354 ms
6,660 KB
testcase_17 AC 372 ms
6,720 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 357 ms
6,520 KB
testcase_23 AC 262 ms
5,652 KB
testcase_24 AC 376 ms
6,908 KB
testcase_25 AC 345 ms
6,436 KB
testcase_26 AC 359 ms
6,444 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 1 ms
4,368 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 2 ms
4,372 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 105 ms
15,264 KB
testcase_39 AC 118 ms
7,180 KB
testcase_40 AC 264 ms
5,920 KB
testcase_41 AC 272 ms
6,924 KB
testcase_42 AC 276 ms
6,980 KB
testcase_43 AC 232 ms
9,136 KB
testcase_44 AC 263 ms
7,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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