結果

問題 No.1514 Squared Matching
ユーザー 👑 emthrmemthrm
提出日時 2021-05-21 21:56:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,410 ms / 4,000 ms
コード長 2,666 bytes
コンパイル時間 2,336 ms
コンパイル使用メモリ 201,276 KB
実行使用メモリ 411,984 KB
最終ジャッジ日時 2024-04-18 15:20:12
合計ジャッジ時間 43,044 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,728 KB
testcase_01 AC 2,353 ms
411,984 KB
testcase_02 AC 5 ms
9,700 KB
testcase_03 AC 4 ms
9,536 KB
testcase_04 AC 4 ms
9,576 KB
testcase_05 AC 5 ms
9,708 KB
testcase_06 AC 33 ms
15,836 KB
testcase_07 AC 406 ms
86,540 KB
testcase_08 AC 2,257 ms
397,160 KB
testcase_09 AC 1,980 ms
348,616 KB
testcase_10 AC 2,141 ms
376,788 KB
testcase_11 AC 2,213 ms
392,180 KB
testcase_12 AC 2,303 ms
402,584 KB
testcase_13 AC 2,329 ms
407,944 KB
testcase_14 AC 2,355 ms
410,144 KB
testcase_15 AC 2,354 ms
411,004 KB
testcase_16 AC 2,367 ms
411,476 KB
testcase_17 AC 2,351 ms
411,848 KB
testcase_18 AC 2,410 ms
411,872 KB
testcase_19 AC 2,368 ms
411,752 KB
testcase_20 AC 2,338 ms
411,892 KB
testcase_21 AC 2,350 ms
411,752 KB
testcase_22 AC 422 ms
90,092 KB
testcase_23 AC 908 ms
170,776 KB
testcase_24 AC 1,395 ms
251,056 KB
testcase_25 AC 1,895 ms
331,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

std::vector<int> prime_sieve(int n, bool get_only_prime) {
  std::vector<int> prime, smallest_prime_factor(n + 1);
  std::iota(smallest_prime_factor.begin(), smallest_prime_factor.end(), 0);
  for (int i = 2; i <= n; ++i) {
    if (smallest_prime_factor[i] == i) prime.emplace_back(i);
    for (int p : prime) {
      if (i * p > n || p > smallest_prime_factor[i]) break;
      smallest_prime_factor[i * p] = p;
    }
  }
  return get_only_prime ? prime : smallest_prime_factor;
}

struct osa_k {
  std::vector<int> smallest_prime_factor;

  osa_k(int n = 10000000) : smallest_prime_factor(prime_sieve(n, false)) {}

  std::vector<std::pair<int, int>> query(int n) const {
    std::vector<std::pair<int, int>> res;
    while (n > 1) {
      int prime = smallest_prime_factor[n], exponent = 0;
      while (smallest_prime_factor[n] == prime) {
        ++exponent;
        n /= prime;
      }
      res.emplace_back(prime, exponent);
    }
    return res;
  }
};

int main() {
  constexpr int N = 50000000;
  int n; cin >> n;
  bitset<N + 1> is_sq = 0;
  vector<int> sq;
  for (int i = 1; i * i <= n; ++i) {
    is_sq.set(i * i);
    sq.emplace_back(i * i);
  }
  vector<int> spf = prime_sieve(n, false);
  int p = int(sq.size()) - 1;
  ll ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (i == 1 || !is_sq[i]) {
      bool is_valid = true;
      int tmp = i;
      while (tmp > 1) {
        int prime = spf[tmp], ex = 0;
        while (spf[tmp] == prime && ex <= 1) {
          ++ex;
          tmp /= prime;
        }
        if (ex > 1) {
          is_valid = false;
          break;
        }
      }
      if (is_valid) {
        while (p >= 0 && 1LL * i * sq[p] > n) --p;
        ans += 1LL * (p + 1) * (p + 1);
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
0