QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#860328 | #9923. Ma Meilleure Ennemie | hos_lyric | AC ✓ | 228ms | 5120kb | C++14 | 7.8kb | 2025-01-18 12:36:04 | 2025-01-18 12:36:13 |
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 = 998244353;
using Mint = ModInt<MO>;
template <class T> T power(T a, long long e, T m) {
for (T b = 1; ; (a *= a) %= m) {
if (e & 1) (b *= a) %= m;
if (!(e >>= 1)) return b;
}
}
long long gcd(long long a, long long b) {
if (a < 0) a = -a;
if (b < 0) b = -b;
if (a == 0) return b;
if (b == 0) return a;
const int s = __builtin_ctzll(a | b);
a >>= __builtin_ctzll(a);
do {
b >>= __builtin_ctzll(b);
if (a > b) swap(a, b);
b -= a;
} while (b);
return a << s;
}
bool isPrime(long long n) {
if (n <= 1 || n % 2 == 0) return (n == 2);
const int s = __builtin_ctzll(n - 1);
const long long d = (n - 1) >> s;
// http://miller-rabin.appspot.com/
for (const long long base : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
__int128 a = base % n;
if (a == 0) continue;
a = power<__int128>(a, d, n);
if (a == 1 || a == n - 1) continue;
bool ok = false;
for (int i = 0; i < s - 1; ++i) {
(a *= a) %= n;
if (a == n - 1) {
ok = true;
break;
}
}
if (!ok) return false;
}
return true;
}
// n >= 3, odd
void factorizeRec(long long n, vector<long long> &ps) {
static constexpr int BLOCK = 256;
if (isPrime(n)) {
ps.push_back(n);
} else {
for (long long c = 2; ; ++c) {
long long x, y = 2, y0, z = 1, d = 1;
for (int l = 1; d == 1; l <<= 1) {
x = y;
for (int i = 0; i < l; ++i) y = (static_cast<__int128>(y) * y + c) % n;
for (int i = 0; i < l; i += BLOCK) {
y0 = y;
for (int j = 0; j < BLOCK && j < l - i; ++j) {
y = (static_cast<__int128>(y) * y + c) % n;
z = (static_cast<__int128>(z) * (y - x)) % n;
}
if ((d = gcd(z, n)) != 1) break;
}
}
if (d == n) {
for (y = y0; ; ) {
y = (static_cast<__int128>(y) * y + c) % n;
if ((d = gcd(y - x, n)) != 1) break;
}
}
if (d != n) {
factorizeRec(d, ps);
factorizeRec(n / d, ps);
return;
}
}
}
}
vector<pair<long long, int>> factorize(long long n) {
vector<pair<long long, int>> pes;
if (n >= 2) {
const int s = __builtin_ctzll(n);
if (s) pes.emplace_back(2, s);
if (n >> s >= 2) {
vector<long long> ps;
factorizeRec(n >> s, ps);
std::sort(ps.begin(), ps.end());
const int psLen = ps.size();
for (int i = 0, j = 0; i < psLen; i = j) {
for (; j < psLen && ps[i] == ps[j]; ++j) {}
pes.emplace_back(ps[i], j - i);
}
}
}
return pes;
}
int main() {
Int N, M;
for (; ~scanf("%lld%lld", &N, &M); ) {
auto PE = factorize(N);
reverse(PE.begin(), PE.end());
// cerr<<"PE = "<<PE<<endl;
const int L = PE.size();
vector<Int> P(L);
vector<int> E(L), EE(L + 1);
EE[0] = 1;
for (int i = 0; i < L; ++i) {
P[i] = PE[i].first;
E[i] = PE[i].second;
EE[i + 1] = EE[i] * (E[i] + 1);
}
vector<Int> D(EE[L]);
D[0] = 1;
for (int i = 0; i < L; ++i) {
for (int u = EE[i]; u < EE[i + 1]; ++u) {
D[u] = D[u - EE[i]] * P[i];
}
}
// cerr<<"D = "<<D<<endl;
vector<Mint> W(EE[L]);
for (int u = 0; u < EE[L]; ++u) {
W[u] = M / D[u];
}
for (int i = 0; i < L; ++i) {
for (int u = 0; u < EE[L]; ++u) if (D[u] % P[i] == 0) {
W[u - EE[i]] -= W[u];
}
}
// cerr<<"W = "<<W<<endl;
auto dp = W;
vector<Mint> work(1 << L);
vector<int> mask(1 << L);
for (int u = EE[L]; --u >= 0; ) {
// kubaru
int len = 1;
work[0] = dp[u];
mask[0] = 0;
for (int i = 0; i < L; ++i) if (D[u] % P[i] == 0) {
for (int h = 0; h < len; ++h) {
work[len + h] = work[h].pow(P[i]);
mask[len + h] = mask[h] + EE[i];
}
len <<= 1;
}
for (int h = 1; h < len; ++h) {
const int v = u - mask[h];
dp[v] -= (__builtin_parity(h)?-1:+1) * work[h];
}
}
// cerr<<"dp = "<<dp<<endl;
printf("%u\n", dp[0].x);
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3840kb
input:
4 4
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
2338 1470
output:
18530141
result:
ok 1 number(s): "18530141"
Test #3:
score: 0
Accepted
time: 228ms
memory: 4864kb
input:
941958815880242160 945059392259579928
output:
57894579
result:
ok 1 number(s): "57894579"
Test #4:
score: 0
Accepted
time: 210ms
memory: 5120kb
input:
876240758958364800 893076802030549616
output:
620071951
result:
ok 1 number(s): "620071951"
Test #5:
score: 0
Accepted
time: 213ms
memory: 4736kb
input:
784965679900201800 821160182532263553
output:
66266543
result:
ok 1 number(s): "66266543"
Test #6:
score: 0
Accepted
time: 184ms
memory: 4864kb
input:
511140442725712800 686753968601283360
output:
297358720
result:
ok 1 number(s): "297358720"
Test #7:
score: 0
Accepted
time: 170ms
memory: 5120kb
input:
897612484786617600 946301485716311910
output:
898294924
result:
ok 1 number(s): "898294924"
Test #8:
score: 0
Accepted
time: 209ms
memory: 4992kb
input:
876240758958364800 949973670837969766
output:
258455620
result:
ok 1 number(s): "258455620"
Test #9:
score: 0
Accepted
time: 199ms
memory: 4992kb
input:
657180569218773600 863561658282273171
output:
674933697
result:
ok 1 number(s): "674933697"
Test #10:
score: 0
Accepted
time: 56ms
memory: 4096kb
input:
9350130049860600 186648010357925450
output:
70597352
result:
ok 1 number(s): "70597352"
Test #11:
score: 0
Accepted
time: 32ms
memory: 4224kb
input:
890488576177200 656051601794505564
output:
18986311
result:
ok 1 number(s): "18986311"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
301180038799975436 468464504626007448
output:
288952066
result:
ok 1 number(s): "288952066"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
523580256903724660 763948483254956750
output:
809203657
result:
ok 1 number(s): "809203657"
Test #14:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
789351011022563115 821578006156306391
output:
498840902
result:
ok 1 number(s): "498840902"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
999999999999999737 999999999999999992
output:
716070890
result:
ok 1 number(s): "716070890"
Test #16:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
999999999999999967 999999999999999976
output:
716070874
result:
ok 1 number(s): "716070874"
Test #17:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
999999874000003969 999999879650092039
output:
877014122
result:
ok 1 number(s): "877014122"
Test #18:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
999999274000130869 999999780452875480
output:
492898975
result:
ok 1 number(s): "492898975"
Test #19:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
999941001154992503 999975909388637582
output:
155276407
result:
ok 1 number(s): "155276407"
Test #20:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
999815008942884521 999872058299052785
output:
547534325
result:
ok 1 number(s): "547534325"
Test #21:
score: 0
Accepted
time: 169ms
memory: 5120kb
input:
897612484786617600 952878466220498188
output:
294147460
result:
ok 1 number(s): "294147460"
Test #22:
score: 0
Accepted
time: 154ms
memory: 5120kb
input:
961727662271376000 974988801671467969
output:
572742162
result:
ok 1 number(s): "572742162"
Test #23:
score: 0
Accepted
time: 209ms
memory: 5120kb
input:
876240758958364800 893903040913665744
output:
435836298
result:
ok 1 number(s): "435836298"
Test #24:
score: 0
Accepted
time: 169ms
memory: 5120kb
input:
897612484786617600 952878466220498188
output:
294147460
result:
ok 1 number(s): "294147460"
Test #25:
score: 0
Accepted
time: 108ms
memory: 4992kb
input:
970391875444992000 980657019550811949
output:
815016337
result:
ok 1 number(s): "815016337"
Test #26:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
1 1000000000000000000
output:
716070898
result:
ok 1 number(s): "716070898"
Test #27:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
916327 9000044616510005
output:
329767168
result:
ok 1 number(s): "329767168"
Test #28:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
7163391 998244353998244353
output:
438645041
result:
ok 1 number(s): "438645041"
Test #29:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
4033 4090
output:
392924428
result:
ok 1 number(s): "392924428"
Extra Test:
score: 0
Extra Test Passed