結果

問題 No.917 Make One With GCD
ユーザー ningenMeningenMe
提出日時 2019-09-09 06:20:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,412 bytes
コンパイル時間 1,808 ms
コンパイル使用メモリ 175,896 KB
実行使用メモリ 401,920 KB
最終ジャッジ日時 2023-09-10 09:09:01
合計ジャッジ時間 14,656 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 8 ms
18,788 KB
testcase_02 AC 8 ms
18,784 KB
testcase_03 AC 75 ms
167,228 KB
testcase_04 AC 61 ms
143,792 KB
testcase_05 AC 136 ms
307,852 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 14 ms
34,148 KB
testcase_28 AC 19 ms
42,468 KB
testcase_29 AC 20 ms
42,144 KB
testcase_30 AC 39 ms
89,200 KB
testcase_31 AC 12 ms
26,548 KB
testcase_32 AC 18 ms
42,468 KB
testcase_33 AC 15 ms
34,272 KB
testcase_34 AC 11 ms
26,480 KB
testcase_35 AC 22 ms
50,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}


//divisor O(sqrt(N))
set<long long> Divisor(long long N) {
    set<long long> ret;
    for (long long i = 1; i*i <= N; ++i) {
        if (N%i == 0) {
            ret.insert(i);
            ret.insert(N / i);
        }
    }
    return ret;
}

//Greatest Common Divisor
long long GCD(long long a, long long b) {
    return ((b == 0) ? a : GCD(b, a % b));
}

//GCD随時計算ver. logで落ちそう
int main() {
	int N,MAX_A = 1000000; cin >> N;
	vector<int> A(N);
	for(int i = 0; i < N; ++i) cin >> A[i];

	// dp[i][j] := (最後にA[i]を使ってgcdがjになるような場合の数) 
	vector<vector<long long>> dp(N, vector<long long>(MAX_A+1,0));

	long long ans = 0;

	//O( N*sqrt(A)+N*N*M*log(A)*log(N*M) )
	for(int i = 0; i < N; ++i) {
		dp[i][A[i]]++;

		// O(sqrt(A))
		auto d = Divisor(A[i]);

		// O(N*M*log(A))
		for(int j = i+1; j < N; ++j) {
			for(auto k:d){
				dp[j][GCD(k,A[j])] += dp[i][k];
			}
		}
		ans += dp[i][1];
	}
	cout << ans << endl;
	return 0;
}
0