結果

問題 No.2620 Sieve of Coins
ユーザー ecotteaecottea
提出日時 2024-01-27 17:23:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 13,982 bytes
コンパイル時間 4,925 ms
コンパイル使用メモリ 288,540 KB
実行使用メモリ 43,308 KB
最終ジャッジ日時 2024-01-27 17:23:14
合計ジャッジ時間 8,718 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,480 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 TLE -
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 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;	using vvvvi = vector<vvvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;	using vvvvl = vector<vvvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define YES(b) {cout << ((b) ? "YES\n" : "NO\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i >= 0; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 矩形内判定

// 汎用関数の定義
template <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

#endif // 折りたたみ用


#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;

#ifdef _MSC_VER
#include "localACL.hpp"
#endif

//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);

namespace atcoder {
	inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
	inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>;
#endif


#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }
#endif


ll solve_small_L(ll l, int n, vl a) {
	vi c(l + 1);
	repe(x, a) c[x] = 1;

	repi(i, 1, l) {
		if (!c[i]) continue;

		for (int x = i * 2; x <= l; x += i) {
			c[x] ^= 1;
		}
	}
	dump(c);

	return accumulate(all(c), 0LL);
}


//【メビウス関数】O(√n)
/*
* メビウス関数の値 μ(n) を返す.
* μ(n) = (-1)^k(n が相異なる k 個の素数の積)or 0(n が平方因子を含む)
*/
int mobius_mu(ll n) {
	// verify : https://algo-method.com/tasks/494

	int res = 1;

	for (ll i = 2; i * i <= n; i++) {
		if (n % (i * i) == 0) return 0;

		if (n % i == 0) {
			n /= i;
			res *= -1;
		}
	}
	if (n > 1) res *= -1;

	return res;
}


void zikken() {
	int n = 1000;
	vi c(n + 1);
	c[1] = 1;

	repi(i, 1, n) {
		if (!c[i]) continue;

		for (int x = i * 2; x <= n; x += i) {
			c[x] ^= 1;
		}
	}

	repi(i, 1, n) {
//		cout << c[i] << " " << mobius_mu(i) << endl;
		assert(c[i] == abs(mobius_mu(i)));
	}

	exit(0);
}


//【ディリクレ畳込みの累積和(乗法的,一括)】(の改変)
/*
* Multiplicative_dirichlet_convolution_acc<T>(int p_max) : O(p_max log(log p_max))
*	p_max ≧ nl 以下の素数を持って初期化する.
*	乗法的数論関数 a[1..n] と数論関数 b[1..n] のディリクレ畳込みを c[1..n] とする.
*	nl, nh は nh ≦ nl ≦ n ≦ nl nh を満たすとし,al, bl, cl, Ah, Bh, Ch は以下の通りとする:
*		al[i] = a[i]		(i∈[1..nl]),  bl, cl も同様
*		Ah[i] = Σa[1..n/i]	(i∈[1..nh]),  Bh, Ch も同様
*
* conv_acc(ll n, vT al, vT Ah, vT bl, vT Bh, vT& cl, vT& Ch) : O(nl log(log nl) + √(n nh))
*	上記 al, Ah, bl, Bh をもとに cl, Ch を計算し格納する.
*
* inv_conv_acc(ll n, vT al, vT Ah, vT cl, vT Ch, vT& bl, vT& Bh) : O(nl log(log nl) + √(n nh))
*	上記 al, Ah, cl, Ch をもとに bl, Bh を計算し格納する.
*
* 特に nl = (n / log(log n))^(2/3) と選ぶと全体の計算量は O(n^(2/3) (log(log n))^(1/3)) になる.
	int n_max = (int)1e8;
	int nl = min((int)pow(n_max / log(log(n_max)), 2. / 3), (int)n);
	int nh = min((int)n / nl + 1, nl);
*/
template <class T>
class Multiplicative_dirichlet_convolution_acc {
	// 参考 : https://maspypy.com/dirichlet-%e7%a9%8d%e3%81%a8%e3%80%81%e6%95%b0%e8%ab%96%e9%96%a2%e6%95%b0%e3%81%ae%e7%b4%af%e7%a9%8d%e5%92%8c

	int p_max;
	vi ps; // 素数のリスト

public:
	// nl 以下の素数を持って初期化する.
	Multiplicative_dirichlet_convolution_acc(int p_max) : p_max(p_max) {
		// verify : https://judge.yosupo.jp/problem/sum_of_totient_function

		// is_prime[i] : i が素数か
		vb is_prime(p_max + 1, true);
		is_prime[0] = is_prime[1] = false;
		int i = 2;

		// √p_max 以下の i の処理
		for (; i <= p_max / i; i++) if (is_prime[i]) {
			ps.push_back(i);
			for (int j = i * i; j <= p_max; j += i) is_prime[j] = false;
		}

		// √p_max より大きい i の処理
		for (; i <= p_max; i++) if (is_prime[i]) ps.push_back(i);
	}

	// al, Ah, bl, Bh をもとに cl, Ch を計算し格納する.
	void conv_acc(ll n, const vector<T>& al, const vector<T>& Ah,
		const vector<T>& bl, const vector<T>& Bh, vector<T>& cl, vector<T>& Ch)
	{
		int nl = sz(al) - 1, nh = sz(Ah) - 1;
		Assert(nl <= p_max); Assert(nh <= nl); Assert(nl <= n); Assert(n <= (ll)nl * nh);

		cl = bl, Ch.assign(nh + 1, 0);

		// cl[1..nl] を計算する.
		repe(p, ps) repir(j, nl / p, 1) {
			for (ll i = p; i * j <= nl; i *= p) cl[i * j] += al[i] * cl[j];
		}

		// Al[i] = Σa[1..i], Bl[i] = Σb[1..i]
		vector<T> Al(nl + 1), Bl(nl + 1);
		repi(i, 1, nl) {
			Al[i] = Al[i - 1] + al[i];
			Bl[i] = Bl[i - 1] + bl[i];
		}

		auto get_Ah = [&](ll i) { return i <= nh ? Ah[i] : Al[n / i]; };
		auto get_Bh = [&](ll i) { return i <= nh ? Bh[i] : Bl[n / i]; };

		// 各 Ch[k] を平方分割で計算する.
		repi(k, 1, nh) {
			int m = (int)(sqrt(n / k) + 1e-12);
			repi(i, 1, m) Ch[k] += al[i] * get_Bh((ll)k * i);
			repi(j, 1, m) Ch[k] += bl[j] * (get_Ah((ll)k * j) - Al[m]);
		}
	}

	// al, Ah, cl, Ch をもとに bl, Bh を計算し格納する.
	void inv_conv_acc(ll n, const vector<T>& al, const vector<T>& Ah,
		vector<T>& bl, vector<T>& Bh)
	{
		// verify : https://judge.yosupo.jp/problem/sum_of_totient_function

		Assert(al[1] != 0);
		int nl = sz(al) - 1, nh = sz(Ah) - 1;
		Assert(nl <= p_max); Assert(nh <= nl); Assert(nl <= n); Assert(n <= (ll)nl * nh);
		
		// bl[1..nl] を計算する.
		repe(p, ps) repi(j, 1, nl / p) {
			bl[j] /= al[1];
			for (ll i = p; i * j <= nl; i *= p) bl[i * j] -= al[i] * bl[j];
		}

		// Al[i] = Σa[1..i], Bl[i] = Σb[1..i]
		vector<T> Al(nl + 1), Bl(nl + 1);
		repi(i, 1, nl) {
			Al[i] = Al[i - 1] + al[i];
			Bl[i] = Bl[i - 1] + bl[i];
		}

		auto get_Ah = [&](ll i) { return i <= nh ? Ah[i] : Al[n / i]; };
		auto get_Bh = [&](ll i) { return i <= nh ? Bh[i] : Bl[n / i]; };

		// 各 Bh[k] を平方分割で計算する.
		repir(k, nh, 1) {
			int m = (int)(sqrt(n / k) + 1e-12);
			repi(i, 2, m) Bh[k] -= al[i] * get_Bh((ll)k * i);
			repi(j, 1, m) Bh[k] -= bl[j] * (get_Ah((ll)k * j) - Al[m]);
			Bh[k] /= al[1];
		}
	}
};


//【無平方数の数え上げ(一括)】O(n^(2/3) log(log n)^(1/3))(の改変)
/*
* 各 i∈[1..nl] について bl[i] = μ(i)^2 を,
* 各 i∈[1..nh] について Bh[i] = Σj∈[1..n/i] μ(j)^2 をそれぞれ格納する.
*
* 制約:nh ≦ nl ≦ n ≦ nl nh
*
* 利用:【ディリクレ畳込みの累積和(乗法的,一括)】
*/
ll square_free_sum(ll n, int nl, int nh) {
	// 参考 : https://maspypy.com/dirichlet-%e7%a9%8d%e3%81%a8%e3%80%81%e6%95%b0%e8%ab%96%e9%96%a2%e6%95%b0%e3%81%ae%e7%b4%af%e7%a9%8d%e5%92%8c
	
	//【方法】
	// 無平方数の指示関数 μ(i)^2 と対応するディリクレ級数を M(s) とおくと,
	//		M(s) = Π_p (1 + 1/p^s)
	//		     = Π_p (1 - 1/p^(2s)) / Π_p (1 - 1/p^s)
	//		     = Π_p 1/(1 - 1/p^s) / Π_p 1/(1 - 1/p^(2s))
	//			 = ζ(s) / ζ(2s)
	// が成り立つ.
	// ζ(2s) は乗法的数論関数 a[i] = (i は平方数 ? 1 : 0) に対応するディリクレ級数であり,
	// ζ(s) は数論関数 c[i] = 1 に対応するディリクレ級数である.

	if (nl <= 0 || nh <= 0) return 0;
	Multiplicative_dirichlet_convolution_acc<ll> M(nl);

	vl al(nl + 1), Ah(nh + 1);
	ll x = 1;
	repi(i, 1, nl) {
		if (i == x * x) {
			al[i] = 1;
			x++;
		}
	}
	while (x * x >= n / nh) x--;
	repir(i, nh, 1) {
		while (n / i >= x * x) x++;
		Ah[i] = x - 1;
	}
	
	//vl a(n + 1);
	//rep(i, n) {
	//	if (i * i > n) break;
	//	a[i * i] = 1;
	//}
	//dump(a);

	//repi(i, 1, nl) al[i] = a[i];

	//repi(i, 2, n) a[i] += a[i - 1];
	//dump(a);

	//repir(i, nh, 1) Ah[i] = a[n / i];

	vl cl(nl + 1, 1), Ch(nh + 1);
	cl[0] = 0;
	repi(i, 1, nh) Ch[i] = n / i;

	//cerr << "- "; repi(i, 1, nl) cerr << i << " \n"[i == nl];
	//dump(al); dump(cl);
	//cerr << "- "; repi(i, 1, nh) cerr << n / i << " \n"[i == nh];
	//dump(Ah); dump(Ch);

	M.inv_conv_acc(n, al, Ah, cl, Ch);

	return Ch[1];
}


ll solve_N_is_1_TLE(ll L, int n, vl a) {
	L /= a[0];

	ll nl = (ll)pow(L / 3.31894, 2. / 3) + 1;
	chmin(nl, 30043519LL / 2);
	ll nh = L / nl + 1;
	chmax(nl, nh); chmin(nl, L); chmin(nh, L);
	dump(nl); dump(nh);

	//cout << "- "; repi(i, 1, nl) cout << i << " \n"[i == nl];
	//dump(bl);
	//cout << "- "; repi(i, 1, nh) cout << L / i << " \n"[i == nh];
	//dump(Bh);

	return square_free_sum(L, (int)nl, (int)nh);
}


ll solve_N_is_1(ll n, int hoge, vl a) {
	n /= a[0];

	vl sq; ll i = 2;
	for (; i * i <= n; i++) sq.push_back(i * i);
	sq.push_back(i * i);

	unordered_map<ll, ll> B;
	
	// 除原理
	function<ll(ll)> rf = [&](ll n) {
		if (B.count(n)) return B[n];
//		dump(n);

		ll val = n;
		repe(i, sq) {
			if (i > n) break;
			val -= rf(n / i);
		}

		B[n] = val;
		return val;
	};

	return rf(n);
}


ll solve(ll n, int hoge, vl a) {
	n /= a[0];

	vl sq;
	for (ll i = 1; i * i <= n; i++) sq.push_back(i * i);
	rep(i, sz(sq)) sq.push_back(2 * sq[i]);
	rep(i, sz(sq)) sq.push_back(3 * sq[i]);
	sort(all(sq));
	sq.erase(sq.begin());
	sq.push_back(INFL);

	unordered_map<ll, ll> B;

	// 除原理
	function<ll(ll)> rf = [&](ll n) {
		if (B.count(n)) return B[n];
		
		ll val = n;
		repe(i, sq) {
			if (i > n) break;
			val -= rf(n / i);
		}

		B[n] = val;
		return val;
	};

	map<ll, int, greater<ll>> tbl;
	repe(x, a) tbl[x] = 1;

	for (auto it = tbl.begin(); it != tbl.end(); it++) {
		auto [i, v] = *it;
		tbl[i * 2]++;
		tbl[i * 3]++;
		tbl[i * 6]++;		
	}
	dump(tbl);

	ll res = 0;
	for (auto [i, v] : tbl) {
		if (i <= n && (v & 1)) res += rf(n / i);
	}

	return res;
}


int main() {
	input_from_file("input.txt");
//	output_to_file("output.txt");

//	zikken();

	ll l; int n;
	cin >> l >> n;

	vl a(n);
	cin >> a;

	//if (l <= (int)1e5) {
	//	cout << solve_small_L(l, n, a) << endl;
	//}
	//else if (n == 1) {
	//	cout << solve_N_is_1(l, n, a) << endl;
	//}
	//else {
		cout << solve(l, n, a) << endl;
	
}
0