結果

問題 No.2249 GCDistance
ユーザー shakayamishakayami
提出日時 2023-03-17 22:57:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,866 bytes
コンパイル時間 1,172 ms
コンパイル使用メモリ 64,148 KB
実行使用メモリ 7,392 KB
最終ジャッジ日時 2023-10-18 15:57:13
合計ジャッジ時間 8,502 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:9: warning: "_GNU_SOURCE" redefined
    9 | #define _GNU_SOURCE
      | 
<command-line>: note: this is the location of the previous definition

ソースコード

diff #

#pragma region opt
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
// #pragma GCC optimize("fast-math") 
// #pragma GCC optimize("unroll-loops")
#pragma endregion opt

#pragma region header
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <time.h>
#pragma endregion header

#pragma region type
/* signed integer */
typedef   int8_t      i8;
typedef   int16_t     i16;
typedef   int32_t     i32;
typedef   int64_t     i64;
typedef __int128_t    i128;
/* unsigned integer */
typedef   uint8_t     u8;
typedef   uint16_t    u16;
typedef   uint32_t    u32;
typedef   uint64_t    u64;
typedef __uint128_t   u128;
/* floating point number */
typedef   float       f32;
typedef   double      f64;
typedef   long double f80;
#pragma endregion type

#pragma region macro
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))

#define POPCNT32(a) __builtin_popcount((a))
#define POPCNT64(a) __builtin_popcountll((a))
#define CTZ32(a) __builtin_ctz((a))
#define CLZ32(a) __builtin_clz((a))
#define CTZ64(a) __builtin_ctzll((a))
#define CLZ64(a) __builtin_clzll((a))
#define HAS_SINGLE_BIT32(a) (__builtin_popcount((a)) == (1))
#define HAS_SINGLE_BIT64(a) (__builtin_popcountll((a)) == (1))
#define MSB32(a) ((31) - __builtin_clz((a)))
#define MSB64(a) ((63) - __builtin_clzll((a)))
#define BIT_WIDTH32(a) ((a) ? ((32) - __builtin_clz((a))) : (0))
#define BIT_WIDTH64(a) ((a) ? ((64) - __builtin_clzll((a))) : (0))
#define LSBit(a) ((a) & (-(a)))
#define CLSBit(a) ((a) & ((a) - (1)))
#define BIT_CEIL32(a) ((!(a)) ? (1) : ((POPCNT32(a)) == (1) ? ((1u) << ((31) - CLZ32((a)))) : ((1u) << ((32) - CLZ32(a)))))
#define BIT_CEIL64(a) ((!(a)) ? (1) : ((POPCNT64(a)) == (1) ? ((1ull) << ((63) - CLZ64((a)))) : ((1ull) << ((64) - CLZ64(a)))))
#define BIT_FLOOR32(a) ((!(a)) ? (0) : ((1u) << ((31) - CLZ32((a)))))
#define BIT_FLOOR64(a) ((!(a)) ? (0) : ((1ull) << ((63) - CLZ64((a)))))
#define _ROTL64(x, s) (((x) << ((s) % (64))) | (((x) >> ((64) - ((s) % (64))))))
#define _ROTR64(x, s) (((x) >> ((s) % (64))) | (((x) << ((64) - ((s) % (64))))))
#define ROTL64(x, s) (((s) == (0)) ? (x) : ((((i128)(s)) < (0)) ? (_ROTR64((x), -(s))) : (_ROTL64((x), (s)))))
#define ROTR64(x, s) (((s) == (0)) ? (x) : ((((i128)(s)) < (0)) ? (_ROTL64((x), -(s))) : (_ROTR64((x), (s)))))
#pragma endregion macro

#pragma region io
static inline int read_int(void) {
  // -2147483648 ~ 2147483647 (> 10 ^ 9)
  int c, x = 0, f = 1;
  while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f;
  while (47 < c && c < 58) {
    x = x * 10 + c - 48;
    c = getchar_unlocked();
  }
  return f * x;
}
static inline i32 in_i32(void) {
  // -2147483648 ~ 2147483647 (> 10 ^ 9)
  i32 c, x = 0, f = 1;
  while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f;
  while (47 < c && c < 58) {
    x = x * 10 + c - 48;
    c = getchar_unlocked();
  }
  return f * x;
}
static inline u32 in_u32(void) {
  // 0 ~ 4294967295 (> 10 ^ 9)
  u32 c, x = 0;
  while (c = getchar_unlocked(), c < 48 || c > 57);
  while (47 < c && c < 58) {
    x = x * 10 + c - 48;
    c = getchar_unlocked();
  }
  return x;
}
static inline i64 in_i64(void) {
  // -9223372036854775808 ~ 9223372036854775807 (> 10 ^ 18)
  i64 c, x = 0, f = 1;
  while (c = getchar_unlocked(), c < 48 || c > 57) if (c == 45) f = -f;
  while (47 < c && c < 58) {
    x = x * 10 + c - 48;
    c = getchar_unlocked();
  }
  return f * x;
}
static inline u64 in_u64(void) {
  // 0 ~ 18446744073709551615 (> 10 ^ 19)
  u64 c, x = 0;
  while (c = getchar_unlocked(), c < 48 || c > 57);
  while (47 < c && c < 58) {
    x = x * 10 + c - 48;
    c = getchar_unlocked();
  }
  return x;
}
static inline void write_int_inner(int x) {
  if (x >= 10) write_int_inner(x / 10);
  putchar_unlocked(x - x / 10 * 10 + 48);
}
static inline void write_int(int x) {
  if (x < 0) {
    putchar_unlocked('-');
    x = -x;
  }
  write_int_inner(x);
}
static inline void out_i32_inner(i32 x) {
  if (x >= 10) out_i32_inner(x / 10);
  putchar_unlocked(x - x / 10 * 10 + 48);
}
static inline void out_i32(i32 x) {
  if (x < 0) {
    putchar_unlocked('-');
    x = -x;
  }
  out_i32_inner(x);
}
static inline void out_u32(u32 x) {
  if (x >= 10) out_u32(x / 10);
  putchar_unlocked(x - x / 10 * 10 + 48);
}
static inline void out_i64_inner(i64 x) {
  if (x >= 10) out_i64_inner(x / 10);
  putchar_unlocked(x - x / 10 * 10 + 48);
}
static inline void out_i64(i64 x) {
  if (x < 0) {
    putchar_unlocked('-');
    x = -x;
  }
  out_i64_inner(x);
}
static inline void out_u64(u64 x) {
  if (x >= 10) out_u64(x / 10);
  putchar_unlocked(x - x / 10 * 10 + 48);
}
static inline void NL(void) { putchar_unlocked('\n'); }
static inline void SP(void) { putchar_unlocked(' '); }
#pragma endregion io

#pragma region totient sum
int primes[9592];
static inline int isqrt(i64 n) {
  int x = sqrtl(n);
  return x;
}
static inline i64 divide(i64 n, i64 d) { return (f64)n / d; }
i64 phi(int p, int e) {
  i64 ret = p - 1;
  while (e --> 1) ret *= p;
  return ret;
}
i128 sum_of_totient_function_rec(i64 n, i64 N, int beg, i64 coef, i64 *s1, i128 *l1, int v, int *primes, int p_cnt) {
  if (!coef) return 0;
  i128 ret = (i128)coef * (n > v ? l1[divide(N, n)] : s1[n]);
  for (int i = beg; i < p_cnt; i++) {
    i64 p = primes[i];
    i64 q = p * p;
    if (q > n) break;
    i64 nn = divide(n, q);
    for (int e = 2; nn > 0; nn = divide(nn, p), e++) {
      i64 coeff = phi(p, e) - phi(p, 1) * phi(p, e - 1);
      ret += sum_of_totient_function_rec(nn, N, i + 1, coef * coeff, s1, l1, v, primes, p_cnt);
    }
  }
  return ret;
}
i64 sum_of_totient_function(i64 N) {
  const int v = isqrt(N);
  int p_cnt = 0;
  i64 *s0 = (i64 *)calloc(v + 1, sizeof(i64));
  i64 *s1 = (i64 *)calloc(v + 1, sizeof(i64));
  i64 *l0 = (i64 *)calloc(v + 1, sizeof(i64));
  i128 *l1 = (i128 *)calloc(v + 1, sizeof(i128));
  for (int i = 1; i <= v; i++) {
    s0[i] = i - 1;
    s1[i] = (i64)i * (i + 1) / 2 - 1;
  }
  for (int i = 1; i <= v; i++) {
    l0[i] = N / i - 1;
    l1[i] = (i128)(N / i) * (N / i + 1) / 2 - 1;
  }
  for (int p = 2; p <= v; p++) if (s0[p] > s0[p - 1]) {
    primes[p_cnt] = p;
    p_cnt++;
    i64 q = (i64)p * p;
    i64 M = N / p;
    i64 t0 = s0[p - 1];
    i64 t1 = s1[p - 1];
    int t = v / p;
    int u = MIN((i64)v, N / q);
    for (int i = 1; i <= t; i++) {
      l0[i] -= (l0[i * p] - t0);
      l1[i] -= (l1[i * p] - t1) * p;
    }
    for (int i = t + 1; i <= u; i++) {
      l0[i] -= (s0[divide(M, i)] - t0);
      l1[i] -= (s1[divide(M, i)] - t1) * p;
    }
    for (int i = v; i >= q; i--) {
      s0[i] -= (s0[divide(i, p)] - t0);
      s1[i] -= (s1[divide(i, p)] - t1) * p;
    }
  }
  for (int i = 1; i <= v; i++) s1[i] -= s0[i];
  for (int i = 1; i <= v; i++) l1[i] -= l0[i];
  for (int pcn = p_cnt - 1; pcn >= 0; pcn--) {
    int p = primes[pcn];
    i64 q = (i64)p * p;
    i64 M = N / p;
    i64 s = s1[p - 1];
    int t = v / p;
    int u = MIN((i64)v, N / q);
    for (i64 i = q; i <= v; i++) s1[i] += (s1[divide(i, p)] - s) * phi(p, 1);
    for (int i = u; i >  t; i--) l1[i] += (s1[divide(M, i)] - s) * phi(p, 1);
    for (int i = t; i >= 1; i--) l1[i] += (l1[i * p] - s) * phi(p, 1);
  }
  for (int i = 1; i <= v; i++) s1[i] += 1;
  for (int i = 1; i <= v; i++) l1[i] += 1;
  i64 ret = sum_of_totient_function_rec(N, N, 0, 1, s1, l1, v, primes, p_cnt);
  free(s0);
  free(s1);
  free(l0);
  free(l1);
  return ret;
}
#pragma endregion totient sum

void Main(void) {
  i64 N = in_i64();
  i64 res=2*sum_of_totient_function(N)-1;
  i64 ans=(2*N*N-res);
  ans-=2*N-1;
  ans/=2;
  out_i64(ans);
  NL();
}

int main(void) {
  i64 T=in_i64();
  for(int i=0;i<T;i++){
      Main();
  }
  return 0;
}
0