結果

問題 No.1036 Make One With GCD 2
ユーザー snrnsidy
提出日時 2021-03-31 21:19:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,023 ms / 2,000 ms
コード長 2,036 bytes
コンパイル時間 1,450 ms
コンパイル使用メモリ 120,644 KB
最終ジャッジ日時 2025-01-20 02:47:09
ジャッジサーバーID
(参考情報)
judge5 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> 
#include <iomanip>
#include <unordered_map>
#include <memory.h>
#include <unordered_set>
#include <fstream>

using namespace std;

#define PI 3.141592653589793
#define MAX 1000000001
const int Mod = 100000007;

long long int tree[1 << 20];

long long int gcd(long long int a, long long int b)
{
	if (b == 0)
	{
		return a;
	}
	return gcd(b, a % b);
}

long long int update(int nodenum, int start, int end)
{
	if (start == end)
	{
		return tree[nodenum];
	}
	int mid = (start + end) / 2;
	return tree[nodenum] = gcd(update(2 * nodenum, start, mid), update(2 * nodenum + 1, mid + 1, end));
}
long long int find(int from, int to, int nodenum, int start, int end)
{
	if (to < start || end < from)
	{
		return 0;
	}
	if (from <= start && end <= to)
	{
		return tree[nodenum];
	}
	int mid = (start + end) / 2;
	return gcd(find(from, to, 2 * nodenum, start, mid), find(from, to, (2 * nodenum + 1), mid + 1, end));
}

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int size, n, a, b;

	cin >> n;

	size = ceil(log2(n));
	size = pow(2, size);

	for (int i = 0; i < n; i++)
	{
		cin >> tree[size + i];
	}
	
	update(1, 1, size);
	long long int res = 0;
	int L = 1;
	int R = 1;
	while (1)
	{
		//cout << L << ' ' << R << '\n';
		if (L > n && R > n)
		{
			break;
		}
		if (R <= n)
		{
			if (find(L, R, 1, 1, size) == 1)
			{
				res += (n - R + 1);
				L++;
			}
			else
			{
				R++;
			}
			if (L > R)
			{
				R++;
			}
		}
		else
		{
			if (find(L, R, 1, 1, size) == 1)
			{
				res += (n - R + 1);
			}
			L++;
		}
	}

	cout << res << '\n';

	return 0;
}
0