QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#93842 | #5817. 小学生数学题 | nhuang685 | 0 | 0ms | 0kb | C++17 | 3.6kb | 2023-04-03 06:45:32 | 2023-04-03 06:45:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
pair<int64_t, int64_t> ex_eucl(int64_t a, int64_t b) {
if (a < b) {
auto [x, y] = ex_eucl(b, a);
return {y, x};
}
if (b == 0) {
assert(a == 1);
return {1, 0};
}
auto [x, y] = ex_eucl(b, a % b);
return {y, x - (a / b) * y};
}
template <class Md>
class Mod {
private:
using T = typename decay<decltype(Md::value)>::type;
T val = 0;
public:
template <class U>
static T normalize(U val) {
if (val <= -Md::value || Md::value <= val) val %= Md::value;
if (val < 0) val += Md::value;
return static_cast<T>(val);
}
Mod() : val(0) {}
template <class U>
Mod(U _val) {
val = normalize(_val);
}
// addition
Mod &operator+=(Mod b) {
val += b.val;
if (val >= Md::value) val -= Md::value;
return *this;
}
friend Mod operator+(Mod a, Mod b) { return (a += b); }
Mod &operator++() { return (*this += 1); }
Mod operator++(int) {
Mod res = *this;
++(*this);
return res;
}
// subtraction
Mod &operator-=(Mod b) {
val -= b.val;
if (val < 0) val += Md::value;
return *this;
}
friend Mod operator-(Mod a, Mod b) { return (a -= b); }
Mod &operator--() { return (*this -= 1); }
Mod operator--(int) {
Mod res = *this;
--(*this);
return res;
}
// multiplication
Mod &operator*=(Mod b) {
val = static_cast<T>(static_cast<int64_t>(val) * b.val % Md::value);
return *this;
}
friend Mod operator*(Mod a, Mod b) { return (a *= b); }
// division
template <class U>
Mod binpow(U b) const {
// assumes Mod(0).binpow(0) == 1
Mod res = 1, a = *this;
while (b > 0) {
if (b % 2 == 1) res *= a;
a *= a;
b /= 2;
}
return res;
}
Mod inv() const {
return Mod(
ex_eucl(static_cast<int64_t>(val), static_cast<int64_t>(Md::value))
.first);
}
Mod operator/=(Mod b) { return (*this *= b.inv()); }
friend Mod operator/(Mod a, Mod b) { return (a /= b); }
// comparison
bool operator==(Mod b) const { return (val == b.val); }
// strong_ordering operator<=>(Mod b) { return (val <=> b.val); }
bool operator!=(Mod b) const { return (val != b.val); }
bool operator<(Mod b) const { return (val < b.val); }
bool operator>(Mod b) const { return (val > b.val); }
bool operator<=(Mod b) const { return (val <= b.val); }
bool operator>=(Mod b) const { return (val >= b.val); }
// io
friend istream &operator>>(istream &in, Mod &a) {
int64_t val;
cin >> val;
a = Mod(val);
return in;
}
friend ostream &operator<<(ostream &out, const Mod a) {
out << a.val;
return out;
}
// conversion
explicit operator T() { return val; }
const T &operator()() { return val; }
};
constexpr int MOD = 998244353;
using Mint = Mod<integral_constant<decay<decltype(MOD)>::type, MOD>>;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
#ifndef LOCAL
// freopen("math.in", "r", stdin);
// freopen("math.out", "w", stdout);
// ifstream cin("math.in");
// ofstream cout("math.out");
#endif
int n, k;
cin >> n >> k;
vector<int64_t> p(n + 1), inv(n + 1);
inv[1] = Mint(1).inv()();
for (int64_t i = 2; i <= n; ++i) {
if (p[i] == 0) {
p[i] = i;
inv[i] = Mint(i).binpow(k).inv()();
for (int64_t j = i * i; j <= n; j += i) p[i] = j;
} else
inv[i] = inv[p[i]] * inv[i / p[i]];
}
Mint fac = 1, ans = 0;
for (int i = 1; i <= n; ++i) {
fac *= i;
ans += fac * inv[i];
}
cout << ans << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
9450395 1
output:
result:
Test #2:
score: 0
Time Limit Exceeded
input:
8978812 1
output:
result:
Test #3:
score: 0
Time Limit Exceeded
input:
8944235 1
output:
result:
Test #4:
score: 0
Time Limit Exceeded
input:
7081118 3
output:
result:
Test #5:
score: 0
Time Limit Exceeded
input:
7904241 3
output:
result:
Test #6:
score: 0
Time Limit Exceeded
input:
9921275 3
output:
result:
Test #7:
score: 0
Time Limit Exceeded
input:
17575748 14135489
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
19858362 14822524
output:
result:
Test #9:
score: 0
Time Limit Exceeded
input:
18848696 15530895
output:
result:
Test #10:
score: 0
Time Limit Exceeded
input:
17787945 13890407