diff --git a/hdu/6889.cpp b/hdu/6889.cpp new file mode 100644 index 0000000..b62be2e --- /dev/null +++ b/hdu/6889.cpp @@ -0,0 +1,118 @@ +#define _CRT_SECURE_NO_WARNINGS +#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING +#include +using namespace std; +#define CRP(t, x) const t &x +#define OPX(op, t, x) operator op(CRP(t, x)) +#define OPL(t, x) bool OPX(<, t, x) const +#define FIL(x, v) memset(x, v, sizeof(x)) +#define CLR(x) FIL(x, 0) +#define NE1(x) FIL(x, -1) +#define INF(x) FIL(x, 0x3f) +#ifndef _DEBUG +#define _DEBUG 0 +#endif // !_DEBUG +#define IFD if (_DEBUG) +typedef int64_t ll, i64; +typedef uint64_t ull, u64; +template +using comtype = typename common_type::type; +template +using enable_if_arithmetic = typename enable_if::value>::type; +template +using enable_if_integral = typename enable_if::value>::type; +inline char getchar(int) +{ + static char buf[64 << 20], *S = buf, *T = buf; + if (S == T) T = fread(S = buf, 1, 64 << 20, stdin) + buf; + return S == T ? EOF : *S++; +} +template > +inline bool read(T &x) +{ + int ch = x = 0, f = 1; + while (!isdigit(ch = getchar())) + if (ch == EOF) + return false; + else if (ch == '-') + f = 0; + for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; + return x = f ? x : -x, true; +} +template > +inline bool read(T &x, Args &... args) { return read(x) && read(args...); } +template > +inline TResult mmin(CRP(T1, v1), CRP(T2, v2)) { return min(v1, v2); } +template > +inline TResult mmin(CRP(T, v), const Args &... args) { return min(v, mmin(args...)); } +template > +inline TResult mmax(CRP(T1, v1), CRP(T2, v2)) { return max(v1, v2); } +template > +inline TResult mmax(CRP(T, v), const Args &... args) { return max(v, mmax(args...)); } +inline ll gcd(ll a, ll b) +{ + for (; b; swap(a, b)) a %= b; + return a; +} +inline ll fpow(ll a, ll b, ll m) +{ + ll r = 1; + for (; b; b >>= 1, a = a * a % m) + if (b & 1) r = r * a % m; + return r; +} +constexpr double eps = 1e-8; +inline int sgn(double x) { return x > eps ? 1 : x < -eps ? -1 : 0; } +const int N = 1000010; +int prime[N], id1[N], id2[N], flag[N], ncnt, m; +ll g[N], sum[N], a[N], T, n; +inline int ID(ll x) { return x <= T ? id1[x] : id2[n / x]; } +inline ll calc(ll x) { return x * (x + 1) / 2 - 1; } +inline ll f(ll x) { return x; } +void init() +{ + T = sqrt(n + 0.5); + for (int i = 2; i <= T; i++) + { + if (!flag[i]) prime[++ncnt] = i, sum[ncnt] = sum[ncnt - 1] + i; + for (int j = 1; j <= ncnt && i * prime[j] <= T; j++) + { + flag[i * prime[j]] = 1; + if (i % prime[j] == 0) break; + } + } + for (ll l = 1; l <= n; l = n / (n / l) + 1) + { + a[++m] = n / l; + if (a[m] <= T) + id1[a[m]] = m; + else + id2[n / a[m]] = m; + g[m] = calc(a[m]); + } + for (int i = 1; i <= ncnt; i++) + for (int j = 1; j <= m && (ll)prime[i] * prime[i] <= a[j]; j++) + g[j] = g[j] - (ll)prime[i] * (g[ID(a[j] / prime[i])] - sum[i - 1]); +} + +ll calc1(ll _n, ll mod) +{ + __int128 x = _n; + return ((2 + x + 1) * x / 2 - 4) % mod; +} +int main() +{ + n = 1e10 + 10; + init(); + int T; + ll __N, mod; + read(T); + while (T--) + { + read(__N, mod); + ll ans = calc1(__N, mod); + ans = ans + g[ID(__N + 1)]; + printf("%lld\n", ans % mod); + } + return 0; +} \ No newline at end of file