結果

問題 No.917 Make One With GCD
ユーザー ningenMeningenMe
提出日時 2019-09-13 00:32:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,650 bytes
コンパイル時間 1,875 ms
コンパイル使用メモリ 182,320 KB
実行使用メモリ 12,020 KB
最終ジャッジ日時 2023-09-15 14:31:13
合計ジャッジ時間 8,447 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

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));
}

//半分全列挙解法
int main() {
	cin.tie(0);ios::sync_with_stdio(false);
	int N,MAX_A = 1000; cin >> N;
	vector<int> A(N);
	for(int i = 0; i < N; ++i) cin >> A[i];
	if(N==1){
		cout << (A[0]==1) << endl;
		return 0;
	}

	vector<int> L,R;
	for(int i = 0; i < N/2; ++i) L.push_back(A[i]);
	for(int i = N/2; i < N; ++i) R.push_back(A[i]);
	map<int,long long> cntL,cntR;

	int NL = L.size(), NR = R.size();
	for(int i = 1; i < (1<<NL); ++i){
		int cnt = 0;
		for(int j = 0; j < NL; ++j) if(i&(1<<j)) cnt = GCD(cnt,A[j]);
		cntL[cnt]++;
	}
	for(int i = 1; i < (1<<NR); ++i){
		int cnt = 0;
		for(int j = 0; j < NR; ++j) if(i&(1<<j)) cnt = GCD(cnt,A[j+(N/2)]);
		cntR[cnt]++;
	}
	long long ans = cntL[1]+cntR[1];
	for(auto& e1:cntL){
		for(auto& e2:cntR){
			if(GCD(e1.first,e2.first)==1) ans += e1.second*e2.second;
		}
	}
	cout << ans << endl;

	return 0;
}
0