結果

問題 No.1036 Make One With GCD 2
ユーザー chocorusk
提出日時 2020-04-26 04:37:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 301 ms / 2,000 ms
コード長 2,041 bytes
コンパイル時間 1,449 ms
コンパイル使用メモリ 131,044 KB
最終ジャッジ日時 2025-01-10 01:51:44
ジャッジサーバーID
(参考情報)
judge3 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:79:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   79 |         scanf("%d", &n);
      |         ~~~~~^~~~~~~~~~
main.cpp:82:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   82 |                 scanf("%lld", &a[i]);
      |                 ~~~~~^~~~~~~~~~~~~~~

ソースコード

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