結果

問題 No.1514 Squared Matching
ユーザー nok0
提出日時 2021-04-29 20:18:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,307 ms / 4,000 ms
コード長 1,587 bytes
コンパイル時間 2,113 ms
コンパイル使用メモリ 199,600 KB
最終ジャッジ日時 2025-01-21 02:12:57
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:56:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   56 |         scanf("%d", &n);
      |         ~~~~~^~~~~~~~~~

ソースコード

diff #

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

#pragma region Math Osa_k
struct osa_k {
private:
	std::vector<int> spf, pr;

public:
	osa_k() = default;

	osa_k(int MAX) : spf(MAX + 1) {
		for(int i = 2; i <= MAX; i++) {
			if(spf[i] == 0) {
				spf[i] = i;
				pr.push_back(i);
			}
			for(int j = 0; j < pr.size() and pr[j] <= spf[i] and i * pr[j] <= MAX; j++)
				spf[i * pr[j]] = pr[j];
		}
	}

	std::vector<std::pair<int, int>> prime_factorization(int n) {
		std::vector<std::pair<int, int>> divisor;
		if(n == 1) return divisor;
		int before = spf[n], cnt = 0;
		while(n > 1) {
			if(spf[n] == before) {
				cnt++;
				n /= spf[n];
			} else {
				divisor.emplace_back(before, cnt);
				before = spf[n];
				cnt = 1;
				n /= spf[n];
			}
		}
		divisor.emplace_back(before, cnt);
		return divisor;
	}

	int smallestprimefactor(const int n) const {
		return spf[n];
	}

	bool is_prime(const int n) const {
		return n == spf[n];
	}
};
#pragma endregion

int embed[6] = {0, 99371160, 207162592, 318140852, 431159896, 545761220};

int n, res;
int main() {
	scanf("%d", &n);
	osa_k pf(n);

	/*
	愚直
	for(int a = 1; a <= n; a++) {
		auto v = pf.prime_factorization(a);
		int b = 1;
		for(auto [val, cnt] : v)
			if(cnt & 1) b *= val;
		res += sqrt(n / b);
	}
	printf("%d\n", res);
	*/

	int base = n / 10000000 * 10000000;
	res = embed[n / 10000000];
	for(int a = base + 1; a <= n; a++) {
		auto v = pf.prime_factorization(a);
		int b = 1;
		for(auto [val, cnt] : v)
			if(cnt & 1) b *= val;
		int d = sqrt(n / b);
		res += d + min(d, (int)sqrt(base / b));
	}

	printf("%d\n", res);
}
0