QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#736217 | #9612. 月亮的背面是粉红色的 | hos_lyric | 100 ✓ | 2269ms | 901724kb | C++14 | 8.1kb | 2024-11-12 06:45:08 | 2024-11-12 06:45:09 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 1000000007;
using Mint = ModInt<MO>;
// \sum[1<=i<=N] floor(N/i)
__int128 Sigma0(unsigned long long N) {
if (N == 0) return 0;
const unsigned long long N2 = sqrt(static_cast<long double>(N));
unsigned long long N3 = cbrt(static_cast<long double>(N));
for (; static_cast<unsigned __int128>(N3) * N3 * N3 < N; ++N3) {}
for (; static_cast<unsigned __int128>(N3) * N3 * N3 > N; --N3) {}
__int128 sum = 0;
// \sum[1<=y<=floor(N^(1/2))] floor(N/y)
using Pt = pair<unsigned long long, unsigned long long>;
// { (x, y) | x y > N }: convex region
auto inside = [&](unsigned long long x, unsigned long long y) -> bool {
return (static_cast<unsigned __int128>(x) * y > N);
};
// -(slope at x) <= p.y/p.x
auto steep = [&](unsigned long long x, const Pt &p) -> bool {
return (static_cast<unsigned __int128>(N) * p.first <= static_cast<unsigned __int128>(p.second) * x * x);
};
vector<Pt> stack{{1, 0}, {1, 1}};
// (x, y): inside
unsigned long long x = N / N2, y = N2 + 1;
for (; ; ) {
Pt l = stack.back();
stack.pop_back();
for (; inside(x + l.first, y - l.second); x += l.first, y -= l.second) {
// (x, y) -> (x + l.x, y - l.y)
// sum for [y - l.y, y)
sum += x * l.second + (l.first - 1) * (l.second + 1) / 2;
}
if (y <= N3) break;
Pt r = l;
for (; l = stack.back(), !inside(x + l.first, y - l.second); stack.pop_back()) r = l;
// (x, y) + l: inside, (x, y) + r: outside
for (; ; ) {
const Pt m(l.first + r.first, l.second + r.second);
if (inside(x + m.first, y - m.second)) {
stack.push_back(l = m);
} else {
if (steep(x + m.first, l)) break;
r = m;
}
}
}
for (; --y; ) sum += N / y;
return 2 * sum - N2 * N2;
}
////////////////////////////////////////////////////////////////////////////////
constexpr int LIM_SIEVE = 100'000'000;
constexpr int LIM_SIEVE_PI = 6'000'000;
int lpf[LIM_SIEVE + 1];
int primesLen;
int primes[LIM_SIEVE_PI];
char moe[LIM_SIEVE + 1];
int fs[LIM_SIEVE + 1];
void sieveInit(int N) {
fill(lpf, lpf + (N + 1), 0);
primesLen = 0;
}
void sieve(int N) {
#ifdef LOCAL
sieveInit(N);
#endif
moe[1] = 1;
fs[1] = 1;
for (int n = 2; n <= N; ++n) {
if (lpf[n] == 0) {
lpf[n] = n;
moe[n] = -1;
primes[primesLen++] = n;
fs[n] = 2;
}
for (int i = 0; i < primesLen && primes[i] <= lpf[n] && primes[i] * n <= N; ++i) {
const int p = primes[i];
const int nn = p * n;
lpf[nn] = p;
if (p == lpf[n]) {
moe[nn] = 0;
int e = 1;
int m = n;
do { ++e; m /= p; } while (m % p == 0);
fs[nn] = (e + 1) * fs[m];
} else {
moe[nn] = -moe[n];
fs[nn] = 2 * fs[n];
}
}
}
}
Int gs[32'000'000];
__int128 s1(Int n) {
return (__int128)n * (n + 1) / 2;
}
int main() {
Int N;
int M;
for (; ~scanf("%lld%d", &N, &M); ) {
const int sqrtN = sqrtl(N);
sieve(sqrtN);
cerr<<"|primes| = "<<primesLen<<endl;
cerr<<"fs = ";pv(fs,fs+31);
cerr<<COLOR("95")<<__LINE__<<": "<<clock()<<COLOR()<<endl;
Mint ans[2] = {};
{
for (int n = 1; n <= sqrtN; ++n) fs[n] += fs[n - 1];
for (int a = 1; a <= sqrtN; ++a) if (moe[a]) {
const Int n = N / ((Int)a * a);
Int sum;
if (n <= sqrtN) {
sum = fs[n];
} else {
sum = Sigma0(n);
}
ans[0] += moe[a] * sum;
}
cerr<<COLOR("95")<<__LINE__<<": "<<clock()<<COLOR()<<endl;
}
if (M) {
for (int n = 1; n <= sqrtN; ++n) gs[n] = gs[n - 1] + (Int)n * (fs[n] - fs[n - 1]);
cerr<<COLOR("95")<<__LINE__<<": "<<clock()<<COLOR()<<endl;
for (int a = 1; a <= sqrtN; ++a) if (moe[a]) {
const Int n = N / ((Int)a * a);
__int128 sum;
if (n <= sqrtN) {
sum = gs[n];
} else {
sum = 0;
const int sn = sqrtl(n);
for (int i = 1; i <= sn; ++i) sum += i * s1(n / i);
sum *= 2;
sum -= s1(sn) * s1(sn);
}
ans[1] += moe[a] * Mint(a) * Mint(a) * (unsigned)(sum % MO);
}
cerr<<COLOR("95")<<__LINE__<<": "<<clock()<<COLOR()<<endl;
}
ans[0] *= ans[0];
ans[1] *= ans[1];
printf("%u", ans[0].x);
if (M) printf(" %u", ans[1].x);
puts("");
}
return 0;
}
详细
Subtask #1:
score: 3
Accepted
Test #1:
score: 3
Accepted
time: 0ms
memory: 10100kb
input:
1726 1
output:
84290761 74619067
result:
ok 2 number(s): "84290761 74619067"
Test #2:
score: 3
Accepted
time: 0ms
memory: 10096kb
input:
3608 1
output:
433014481 672891299
result:
ok 2 number(s): "433014481 672891299"
Test #3:
score: 3
Accepted
time: 0ms
memory: 10156kb
input:
2921 1
output:
271096225 547734266
result:
ok 2 number(s): "271096225 547734266"
Subtask #2:
score: 3
Accepted
Test #4:
score: 3
Accepted
time: 1ms
memory: 10028kb
input:
6116899 1
output:
219318963 301450440
result:
ok 2 number(s): "219318963 301450440"
Test #5:
score: 3
Accepted
time: 1ms
memory: 10324kb
input:
6260707 1
output:
148720176 263856753
result:
ok 2 number(s): "148720176 263856753"
Test #6:
score: 3
Accepted
time: 1ms
memory: 10128kb
input:
6763677 1
output:
944542490 136397156
result:
ok 2 number(s): "944542490 136397156"
Subtask #3:
score: 8
Accepted
Test #7:
score: 8
Accepted
time: 3ms
memory: 10532kb
input:
6469467712 0
output:
147393348
result:
ok 1 number(s): "147393348"
Test #8:
score: 8
Accepted
time: 1ms
memory: 10280kb
input:
8967004453 0
output:
229436583
result:
ok 1 number(s): "229436583"
Test #9:
score: 8
Accepted
time: 0ms
memory: 12596kb
input:
6636594384 0
output:
995965072
result:
ok 1 number(s): "995965072"
Subtask #4:
score: 8
Accepted
Test #10:
score: 8
Accepted
time: 6ms
memory: 14940kb
input:
8292948816 1
output:
566765721 287485757
result:
ok 2 number(s): "566765721 287485757"
Test #11:
score: 8
Accepted
time: 6ms
memory: 11188kb
input:
8592748771 1
output:
649470692 164561252
result:
ok 2 number(s): "649470692 164561252"
Test #12:
score: 8
Accepted
time: 6ms
memory: 13416kb
input:
9827380808 1
output:
291159931 188690805
result:
ok 2 number(s): "291159931 188690805"
Subtask #5:
score: 8
Accepted
Test #13:
score: 8
Accepted
time: 44ms
memory: 28124kb
input:
900472451132 1
output:
247050500 963765719
result:
ok 2 number(s): "247050500 963765719"
Test #14:
score: 8
Accepted
time: 40ms
memory: 28140kb
input:
850862494659 1
output:
200210720 915108650
result:
ok 2 number(s): "200210720 915108650"
Test #15:
score: 8
Accepted
time: 39ms
memory: 25596kb
input:
851346512859 1
output:
895785763 504512885
result:
ok 2 number(s): "895785763 504512885"
Subtask #6:
score: 10
Accepted
Test #16:
score: 10
Accepted
time: 147ms
memory: 62952kb
input:
9864907300784 1
output:
359943536 720268421
result:
ok 2 number(s): "359943536 720268421"
Test #17:
score: 10
Accepted
time: 127ms
memory: 59952kb
input:
8181674676063 1
output:
839993102 994056029
result:
ok 2 number(s): "839993102 994056029"
Test #18:
score: 10
Accepted
time: 153ms
memory: 63916kb
input:
9893510217522 1
output:
157499971 930653488
result:
ok 2 number(s): "157499971 930653488"
Subtask #7:
score: 13
Accepted
Test #19:
score: 13
Accepted
time: 214ms
memory: 98480kb
input:
96735749745529 0
output:
223354886
result:
ok 1 number(s): "223354886"
Test #20:
score: 13
Accepted
time: 220ms
memory: 101524kb
input:
95243570720799 0
output:
555372474
result:
ok 1 number(s): "555372474"
Test #21:
score: 13
Accepted
time: 226ms
memory: 97880kb
input:
97668723090105 0
output:
84562124
result:
ok 1 number(s): "84562124"
Subtask #8:
score: 14
Accepted
Test #22:
score: 14
Accepted
time: 490ms
memory: 177232kb
input:
94060593399194 1
output:
52991150 887133157
result:
ok 2 number(s): "52991150 887133157"
Test #23:
score: 14
Accepted
time: 489ms
memory: 180788kb
input:
98527940728119 1
output:
281611635 910356955
result:
ok 2 number(s): "281611635 910356955"
Test #24:
score: 14
Accepted
time: 461ms
memory: 171412kb
input:
90501814019947 1
output:
666385143 229785369
result:
ok 2 number(s): "666385143 229785369"
Subtask #9:
score: 16
Accepted
Test #25:
score: 16
Accepted
time: 2269ms
memory: 898900kb
input:
9772457586483846 0
output:
631039552
result:
ok 1 number(s): "631039552"
Test #26:
score: 16
Accepted
time: 2207ms
memory: 901724kb
input:
9889806164705403 0
output:
169322134
result:
ok 1 number(s): "169322134"
Test #27:
score: 16
Accepted
time: 2163ms
memory: 882336kb
input:
9422498258316766 0
output:
413943782
result:
ok 1 number(s): "413943782"
Test #28:
score: 16
Accepted
time: 2250ms
memory: 900656kb
input:
9845978636381962 0
output:
857401052
result:
ok 1 number(s): "857401052"
Test #29:
score: 16
Accepted
time: 2226ms
memory: 896628kb
input:
9761171951453691 0
output:
205712009
result:
ok 1 number(s): "205712009"
Test #30:
score: 16
Accepted
time: 2173ms
memory: 873508kb
input:
9224667465673566 0
output:
143979878
result:
ok 1 number(s): "143979878"
Subtask #10:
score: 17
Accepted
Test #31:
score: 17
Accepted
time: 1577ms
memory: 526968kb
input:
949243849085176 1
output:
508465534 771553755
result:
ok 2 number(s): "508465534 771553755"
Test #32:
score: 17
Accepted
time: 1571ms
memory: 518412kb
input:
924225524519163 1
output:
867410272 870831653
result:
ok 2 number(s): "867410272 870831653"
Test #33:
score: 17
Accepted
time: 1601ms
memory: 533200kb
input:
978079151303393 1
output:
235076358 675828942
result:
ok 2 number(s): "235076358 675828942"
Test #34:
score: 17
Accepted
time: 1560ms
memory: 520524kb
input:
929804617107620 1
output:
790604296 73162158
result:
ok 2 number(s): "790604296 73162158"
Test #35:
score: 17
Accepted
time: 1587ms
memory: 537884kb
input:
989806727450552 1
output:
378550840 783149232
result:
ok 2 number(s): "378550840 783149232"