QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#748582 | #9619. 乘积,欧拉函数,求和 | xiaolei338 | RE | 73ms | 12040kb | C++23 | 7.6kb | 2024-11-14 20:46:08 | 2024-11-14 20:46:08 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
const int N = 1e6 + 10, mod = 998244353, INF = 0x3f3f3f3f;
random_device rd;
mt19937_64 rng(rd());
LL n, m;
LL a[N];
template<class T>
constexpr T power(T a, LL b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
constexpr LL mul(LL a, LL b, LL p) {
LL res = a * b - LL(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template<LL P>
struct MLong {
LL x;
constexpr MLong() : x{} {}
constexpr MLong(LL x) : x{norm(x % getMod())} {}
static LL Mod;
constexpr static LL getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(LL Mod_) {
Mod = Mod_;
}
constexpr LL norm(LL x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr LL val() const {
return x;
}
explicit constexpr operator LL() const {
return x;
}
constexpr MLong operator-() const {
MLong res;
res.x = norm(getMod() - x);
return res;
}
constexpr MLong inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MLong &operator*=(MLong rhs) & {
x = mul(x, rhs.x, getMod());
return *this;
}
constexpr MLong &operator+=(MLong rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MLong &operator-=(MLong rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MLong &operator/=(MLong rhs) & {
return *this *= rhs.inv();
}
friend constexpr MLong operator*(MLong lhs, MLong rhs) {
MLong res = lhs;
res *= rhs;
return res;
}
friend constexpr MLong operator+(MLong lhs, MLong rhs) {
MLong res = lhs;
res += rhs;
return res;
}
friend constexpr MLong operator-(MLong lhs, MLong rhs) {
MLong res = lhs;
res -= rhs;
return res;
}
friend constexpr MLong operator/(MLong lhs, MLong rhs) {
MLong res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MLong &a) {
LL v;
is >> v;
a = MLong(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a) {
return os << a.val();
}
friend constexpr bool operator==(MLong lhs, MLong rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MLong lhs, MLong rhs) {
return lhs.val() != rhs.val();
}
};
template<>
LL MLong<0LL>::Mod = LL(1E18) + 9;
template<int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(LL x) : x{norm(x % getMod())} {}
static int Mod;
constexpr static int getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_) {
Mod = Mod_;
}
constexpr int norm(int x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const {
return x;
}
explicit constexpr operator int() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
x = 1LL * x * rhs.x % getMod();
return *this;
}
constexpr MInt &operator+=(MInt rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) & {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
LL v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template<>
int MInt<0>::Mod = 998244353;
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 998244353;
using Z = MInt<P>;
LL prime[N], rk[N], tot;
bool vis[N];
vector<LL> fac[N], g[N];
Z inv[N], dp[N], lst[N];
void solve()
{
cin >> n;
for(int i = 1; i <= n; i ++)cin >> a[i];
for(int i = 1; i <= 3000; i ++)inv[i] = (i - 1) * power((Z)i, mod - 2);
for(int i = 2; i <= 3000; i ++)
{
if(!vis[i])prime[++ tot] = i, rk[i] = tot;
for(int j = i + i; j <= 3000; j += i)vis[j] = 1;
}
for(int i = 1; i <= n; i ++)
{
LL tmp = a[i];
for(int j = 1; j <= tot && prime[j] <= a[i]; j ++){
if(a[i] % prime[j] == 0){
fac[i].push_back(prime[j]);
while(a[i] % prime[j] == 0)a[i] /= prime[j];
}
}
a[i] = tmp;
}
dp[0] = 1;
for(int i = 1; i <= n; i ++)
{
if(!fac[i].empty() && fac[i].back() > 53)g[rk[fac[i].back()]].push_back(i);
else{
int now = 0;
for(int j = 0; j < fac[i].size(); j ++)now |= 1 << (rk[fac[i][j]] - 1);
for(int s = (1 << 16) - 1; s >= 0; s --)dp[s | now] += dp[s] * a[i];
}
}
for(int now = 17; now <= tot; now ++)
{
for(int s = 0; s < 1 << 16; s ++)lst[s] = dp[s];
for(int i = 0; i < g[now].size(); i ++)
{
int pos = g[now][i], tmp = 0;
for(int j = 0; j < fac[pos].size(); j ++)tmp |= 1 << (rk[fac[pos][j]] - 1);
for(int s = (1 << 16) - 1; s >= 0; s --)dp[s | tmp] += dp[s] * a[pos];
}
for(int s = 0; s < 1 << 16; s ++)
{
dp[s] -= lst[s];
dp[s] = dp[s] * inv[prime[now]] + lst[s];
}
}
Z ans = 0;
for(int s = 0; s < 1 << 16; s ++)
{
Z mul = 1;
for(int i = 0; i < 16; i ++)
{
if((s >> i) & 1)mul *= inv[prime[i + 1]];
}
ans += mul * dp[s];
}
// cout << (LL)pow(2, 17) << '\n';
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
LL _T = 1;
// cin >> _T;
while(_T --)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 73ms
memory: 11988kb
input:
5 1 6 8 6 2
output:
892
result:
ok single line: '892'
Test #2:
score: 0
Accepted
time: 69ms
memory: 12040kb
input:
5 3 8 3 7 8
output:
3157
result:
ok single line: '3157'
Test #3:
score: -100
Runtime Error
input:
2000 79 1 1 1 1 1 1 2803 1 1 1 1 1 1 1609 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2137 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 613 1 499 1 211 1 2927 1 1 1327 1 1 1123 1 907 1 2543 1 1 1 311 2683 1 1 1 1 2963 1 1 1 641 761 1 1 1 1 1 1 1 1 1 1 1 1489 2857 1 1 1 1 1 1 1 1 1 1 1 1 1 967 1 821 1 1 1 1 2143 1861...