結果

問題 No.1036 Make One With GCD 2
ユーザー chocoruskchocorusk
提出日時 2020-04-26 04:37:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 240 ms / 2,000 ms
コード長 2,041 bytes
コンパイル時間 1,534 ms
コンパイル使用メモリ 134,756 KB
実行使用メモリ 15,388 KB
最終ジャッジ日時 2023-10-14 20:19:01
合計ジャッジ時間 8,740 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
7,608 KB
testcase_01 AC 142 ms
6,976 KB
testcase_02 AC 102 ms
15,288 KB
testcase_03 AC 16 ms
4,384 KB
testcase_04 AC 26 ms
5,676 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 46 ms
4,920 KB
testcase_08 AC 37 ms
4,412 KB
testcase_09 AC 148 ms
7,036 KB
testcase_10 AC 138 ms
6,780 KB
testcase_11 AC 155 ms
6,956 KB
testcase_12 AC 147 ms
6,720 KB
testcase_13 AC 240 ms
6,712 KB
testcase_14 AC 238 ms
6,700 KB
testcase_15 AC 222 ms
6,744 KB
testcase_16 AC 225 ms
6,492 KB
testcase_17 AC 240 ms
6,624 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 224 ms
6,432 KB
testcase_23 AC 167 ms
5,708 KB
testcase_24 AC 234 ms
6,740 KB
testcase_25 AC 211 ms
6,452 KB
testcase_26 AC 215 ms
6,428 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,356 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,368 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 97 ms
15,388 KB
testcase_39 AC 129 ms
6,888 KB
testcase_40 AC 162 ms
5,732 KB
testcase_41 AC 148 ms
6,912 KB
testcase_42 AC 153 ms
7,052 KB
testcase_43 AC 150 ms
9,096 KB
testcase_44 AC 145 ms
7,964 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(a==0) return b;
	if(b==0) return a;
	if(a<0) a=-a;
	if(b<0) b=-b;
	const int s=__builtin_ctzll(a|b);
	a>>=__builtin_ctzll(a);
	while(b){
		b>>=__builtin_ctzll(b);
		if(a>b) swap(a, b);
		b-=a;
	}
	return a<<s;
}
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