QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#875004 | #9923. Ma Meilleure Ennemie | ucup-team5243 | AC ✓ | 275ms | 7912kb | C++17 | 11.2kb | 2025-01-28 23:39:47 | 2025-01-28 23:39:47 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
using namespace std;
#include <initializer_list>
namespace nachia{
bool IsPrime(unsigned long long x) noexcept {
if(x <= 1) return false;
if(x % 2 == 0) return x == 2;
using u64 = unsigned long long;
using u128 = __uint128_t;
u64 d = x-1;
int s = 0;
int q = 63;
while(!(d&1)){ d >>= 1; s++; }
while(!(d >> q)) q--;
u64 r = x; for(int t=0; t<6; t++) r*=2-r*x;
u128 n2 = -(u128)x % x;
auto red = [=](u128 t) noexcept -> u64 {
u64 t2 = (t + (u128)((u64)t*-r)*x) >> 64;
return (t2 >= x || t2 < (t >> 64)) ? t2-x : t2;
};
u64 one = red(n2);
for(u64 base : { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 }){
if(base%x==0) continue;
u64 a = base = red(base%x*n2);
for(int e=q-1; e>=0; e--){ a = red((u128)a*a); if((d>>e)&1) a = red((u128)a*base); }
if(a == one) continue;
for(int t=1; t<s&&a!=x-one; t++) a = red((u128)a*a);
if(a != x-one) return false;
}
return true;
}
} // namespace nachia
namespace nachia{
int Popcount(unsigned long long c) noexcept {
#ifdef __GNUC__
return __builtin_popcountll(c);
#else
c = (c & (~0ull/3)) + ((c >> 1) & (~0ull/3));
c = (c & (~0ull/5)) + ((c >> 2) & (~0ull/5));
c = (c & (~0ull/17)) + ((c >> 4) & (~0ull/17));
c = (c * (~0ull/257)) >> 56;
return c;
#endif
}
// please ensure x != 0
int MsbIndex(unsigned long long x) noexcept {
#ifdef __GNUC__
return 63 - __builtin_clzll(x);
#else
using u64 = unsigned long long;
int q = (x >> 32) ? 32 : 0;
auto m = x >> q;
constexpr u64 hi = 0x88888888;
constexpr u64 mi = 0x11111111;
m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 35;
m = (((m | ~(hi - (m & ~hi))) & hi) * mi) >> 31;
q += (m & 0xf) << 2;
q += 0x3333333322221100 >> (((x >> q) & 0xf) << 2) & 0xf;
return q;
#endif
}
// please ensure x != 0
int LsbIndex(unsigned long long x) noexcept {
#ifdef __GNUC__
return __builtin_ctzll(x);
#else
return MsbIndex(x & -x);
#endif
}
}
namespace nachia{
std::vector<std::pair<unsigned long long, int>> Factorize(unsigned long long x){
if(x == 1) return {};
if(IsPrime(x)) return {{x,1}};
using u64 = unsigned long long;
using u128 = __uint128_t;
u64 X = x;
std::vector<u64> p;
for(u64 i=2; i<100; i+=1+i%2) if(x%i==0){ p.push_back(i); while(x%i==0) x/=i; }
u64 r=1; u128 n2=1;
auto updX = [&](){
r = x; for(int t=0; t<6; t++) r*=2-r*x;
n2 = -(u128)x % x;
};
auto red = [&](u128 t) noexcept -> u64 {
u64 s = ((u128)x*((u64)t*r)) >> 64;
u64 t2 = t >> 64;
return t2-s + (t2 < s ? x : 0);
};
auto mult = [&](u64 a, u64 b) noexcept { return red((u128)red((u128)a*n2)*b); };
auto gcd = [](u64 a, u64 b) noexcept {
if(!a || !b) return a|b;
int q = LsbIndex(a|b);
b >>= LsbIndex(b);
a >>= LsbIndex(a);
while(a!=b){
if(a<b){ b-=a; b>>=LsbIndex(b); }
else{ a-=b; a>>=LsbIndex(a); }
}
return a<<q;
};
static u64 v = 7001;
p.push_back(x);
for(int pi=p.size()-1; pi<(int)p.size(); pi++) while(p[pi] != 1 && !IsPrime(p[pi])){
x = p[pi]; updX();
while(p[pi] == x){
v^=v<<13; v^=v>>7; v^=v<<17; // Xorshift https://www.jstatsoft.org/article/download/v008i14/916
u64 c = red(v); if(c == 0) continue;
auto f = [=](u64 a) noexcept -> u64 { return red((u128)a*a+c); };
u64 a=0, b=f(a);
u64 buf = 1, sz = 1, nx = 10;
while(true){
while(nx != sz && a != b){
buf = mult(buf, a<=b?b-a:a-b); sz++;
a = f(a); b = f(f(b));
}
u64 g = gcd(buf, x);
if(g != 1){
while(p[pi] % g == 0) p[pi] /= g;
p.push_back(g);
break;
}
if(a == b) break;
nx = sz * 3 / 2;
}
}
}
std::vector<std::pair<u64, int>> res;
for(u64 q : p) if(q != 1){
int e=0; while(X%q == 0){ e++; X/=q; }
if(e) res.push_back({ q, e });
}
return res;
}
unsigned long long Totient(unsigned long long x){
auto F = Factorize(x);
for(auto f : F) x -= x / f.first;
return x;
}
} // namespace nachia
namespace nachia{
template<class Int> Int Gcd(Int a, Int b){
if(a < 0) a = -a;
if(b < 0) b = -b;
if(!a || !b) return a + b;
while(b){ a %= b; std::swap(a, b); }
return a;
}
}
namespace nachia{
struct EnumerateDivisors{
using u64 = unsigned long long;
u64 raw;
std::vector<u64> divord;
std::vector<int> dims;
std::vector<int> dimcum;
std::vector<std::pair<u64, int>> I;
std::vector<std::pair<u64, int>> pfs;
EnumerateDivisors(
std::vector<std::pair<unsigned long long, int>> pf
)
: pfs(std::move(pf))
{
raw = 1;
int n = pfs.size();
dims.resize(n);
dimcum.assign(n+1, 1);
divord = {1};
for(int i=0; i<n; i++){
dims[i] = pfs[i].second;
dimcum[i+1] = dimcum[i] * (dims[i] + 1);
int q = dimcum[i];
for(int t=q; t<dimcum[i+1]; t++) divord.push_back(divord[t-q] * pfs[i].first);
for(int t=0; t<pfs[i].second; t++) raw *= pfs[i].first;
}
I.resize(divord.size());
for(int i=0; i<dimcum.back(); i++) I[i] = std::make_pair(divord[i], i);
std::sort(I.begin(), I.end());
}
EnumerateDivisors(unsigned long long n)
: EnumerateDivisors(Factorize(n)) {}
int id(unsigned long long d) const {
d = Gcd(d, raw);
return std::lower_bound(I.begin(), I.end(), d, [](std::pair<u64, int> e, u64 v){ return e.first < v; })->second;
}
int numDivisors() const { return dimcum.back(); }
unsigned long long divisor(int i){ return divord[i]; }
template<class Elem>
void Zeta(std::vector<Elem>& A) const {
int Z = numDivisors();
for(int d=0; d<(int)dims.size(); d++){
int w = dims[d] * dimcum[d];
int y = dimcum[d];
for(int i=0; i<Z; i+=dimcum[d+1]){
for(int j=0; j<w; j++) A[i+j+y] += A[i+j];
}
}
}
template<class Elem>
void RevZeta(std::vector<Elem>& A) const {
int Z = numDivisors();
for(int d=0; d<(int)dims.size(); d++){
int w = dims[d] * dimcum[d];
int y = dimcum[d];
for(int i=0; i<Z; i+=dimcum[d+1]){
for(int j=w-1; j>=0; j--) A[i+j] += A[i+j+y];
}
}
}
template<class Elem>
void Mobius(std::vector<Elem>& A) const {
int Z = numDivisors();
for(int d=0; d<(int)dims.size(); d++){
int w = dims[d] * dimcum[d];
int y = dimcum[d];
for(int i=0; i<Z; i+=dimcum[d+1]){
for(int j=w-1; j>=0; j--) A[i+j+y] -= A[i+j];
}
}
}
template<class Elem>
void RevMobius(std::vector<Elem>& A) const {
int Z = numDivisors();
for(int d=0; d<(int)dims.size(); d++){
int w = dims[d] * dimcum[d];
int y = dimcum[d];
for(int i=0; i<Z; i+=dimcum[d+1]){
for(int j=0; j<w; j++) A[i+j] -= A[i+j+y];
}
}
}
};
}
namespace nachia{
// ax + by = gcd(a,b)
// return ( x, - )
std::pair<long long, long long> ExtGcd(long long a, long long b){
long long x = 1, y = 0;
while(b){
long long u = a / b;
std::swap(a-=b*u, b);
std::swap(x-=y*u, y);
}
return std::make_pair(x, a);
}
} // namespace nachia
namespace nachia{
template<unsigned int MOD>
struct StaticModint{
private:
using u64 = unsigned long long;
unsigned int x;
public:
using my_type = StaticModint;
template< class Elem >
static Elem safe_mod(Elem x){
if(x < 0){
if(0 <= x+MOD) return x + MOD;
return MOD - ((-(x+MOD)-1) % MOD + 1);
}
return x % MOD;
}
StaticModint() : x(0){}
StaticModint(const my_type& a) : x(a.x){}
StaticModint& operator=(const my_type&) = default;
template< class Elem >
StaticModint(Elem v) : x(safe_mod(v)){}
unsigned int operator*() const { return x; }
my_type& operator+=(const my_type& r) { auto t = x + r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
my_type operator+(const my_type& r) const { my_type res = *this; return res += r; }
my_type& operator-=(const my_type& r) { auto t = x + MOD - r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
my_type operator-(const my_type& r) const { my_type res = *this; return res -= r; }
my_type operator-() const noexcept { my_type res = *this; res.x = ((res.x == 0) ? 0 : (MOD - res.x)); return res; }
my_type& operator*=(const my_type& r){ x = (u64)x * r.x % MOD; return *this; }
my_type operator*(const my_type& r) const { my_type res = *this; return res *= r; }
bool operator==(const my_type& r) const { return x == r.x; }
my_type pow(unsigned long long i) const {
my_type a = *this, res = 1;
while(i){ if(i & 1){ res *= a; } a *= a; i >>= 1; }
return res;
}
my_type inv() const { return my_type(ExtGcd(x, MOD).first); }
unsigned int val() const { return x; }
static constexpr unsigned int mod() { return MOD; }
static my_type raw(unsigned int val) { auto res = my_type(); res.x = val; return res; }
my_type& operator/=(const my_type& r){ return operator*=(r.inv()); }
my_type operator/(const my_type& r) const { return operator*(r.inv()); }
};
} // namespace nachia
using Modint = nachia::StaticModint<998244353>;
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
i64 N, M; cin >> N >> M;
auto divs = nachia::EnumerateDivisors(N);
i64 n = divs.numDivisors();
vector<i64> D(n); rep(i,n) D[i] = divs.divisor(i);
auto dims = divs.dimcum;
i64 dimn = dims.size() - 1;
vector<Modint> A(n);
rep(i,n) A[i] = M / D[i];
divs.RevMobius(A);
vector<Modint> C(n); C[0] = -1;
divs.Mobius(C);
vector<Modint> dp(n);
i64 maskx = 1 << dimn;
vector<i64> offset(maskx), offsetv(maskx,1);
rep(t,dimn) rep(i,1<<t) offset[i+(1<<t)] = offset[i] + dims[t];
rep(t,dimn) rep(i,1<<t) offsetv[i+(1<<t)] = offsetv[i] * D[dims[t]];
vector<Modint> buf(1<<dimn);
for(i64 i=n-1; i>=0; i--){
dp[i] += A[i];
if(i == 0) break;
i64 mask = 0;
rep(t,dimn) if(i % dims[t+1] >= dims[t]) mask |= 1ll << t;
buf[0] = dp[i];
for(i64 jx=(mask-1)&mask; ; jx=(jx-1)&mask){
i64 j = mask - jx;
buf[j] = (buf[j-(j&-j)].pow(offsetv[j&-j]));
dp[i-offset[j]] += C[offset[j]] * buf[j];
if(jx == 0) break;
}
}
cout << dp[0].val() << "\n";
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3712kb
input:
4 4
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
2338 1470
output:
18530141
result:
ok 1 number(s): "18530141"
Test #3:
score: 0
Accepted
time: 275ms
memory: 7272kb
input:
941958815880242160 945059392259579928
output:
57894579
result:
ok 1 number(s): "57894579"
Test #4:
score: 0
Accepted
time: 251ms
memory: 7776kb
input:
876240758958364800 893076802030549616
output:
620071951
result:
ok 1 number(s): "620071951"
Test #5:
score: 0
Accepted
time: 256ms
memory: 6884kb
input:
784965679900201800 821160182532263553
output:
66266543
result:
ok 1 number(s): "66266543"
Test #6:
score: 0
Accepted
time: 217ms
memory: 7140kb
input:
511140442725712800 686753968601283360
output:
297358720
result:
ok 1 number(s): "297358720"
Test #7:
score: 0
Accepted
time: 202ms
memory: 7904kb
input:
897612484786617600 946301485716311910
output:
898294924
result:
ok 1 number(s): "898294924"
Test #8:
score: 0
Accepted
time: 251ms
memory: 7780kb
input:
876240758958364800 949973670837969766
output:
258455620
result:
ok 1 number(s): "258455620"
Test #9:
score: 0
Accepted
time: 237ms
memory: 7524kb
input:
657180569218773600 863561658282273171
output:
674933697
result:
ok 1 number(s): "674933697"
Test #10:
score: 0
Accepted
time: 67ms
memory: 5088kb
input:
9350130049860600 186648010357925450
output:
70597352
result:
ok 1 number(s): "70597352"
Test #11:
score: 0
Accepted
time: 37ms
memory: 4480kb
input:
890488576177200 656051601794505564
output:
18986311
result:
ok 1 number(s): "18986311"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
301180038799975436 468464504626007448
output:
288952066
result:
ok 1 number(s): "288952066"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
523580256903724660 763948483254956750
output:
809203657
result:
ok 1 number(s): "809203657"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
789351011022563115 821578006156306391
output:
498840902
result:
ok 1 number(s): "498840902"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
999999999999999737 999999999999999992
output:
716070890
result:
ok 1 number(s): "716070890"
Test #16:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
999999999999999967 999999999999999976
output:
716070874
result:
ok 1 number(s): "716070874"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
999999874000003969 999999879650092039
output:
877014122
result:
ok 1 number(s): "877014122"
Test #18:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
999999274000130869 999999780452875480
output:
492898975
result:
ok 1 number(s): "492898975"
Test #19:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
999941001154992503 999975909388637582
output:
155276407
result:
ok 1 number(s): "155276407"
Test #20:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
999815008942884521 999872058299052785
output:
547534325
result:
ok 1 number(s): "547534325"
Test #21:
score: 0
Accepted
time: 202ms
memory: 7912kb
input:
897612484786617600 952878466220498188
output:
294147460
result:
ok 1 number(s): "294147460"
Test #22:
score: 0
Accepted
time: 185ms
memory: 7652kb
input:
961727662271376000 974988801671467969
output:
572742162
result:
ok 1 number(s): "572742162"
Test #23:
score: 0
Accepted
time: 249ms
memory: 7784kb
input:
876240758958364800 893903040913665744
output:
435836298
result:
ok 1 number(s): "435836298"
Test #24:
score: 0
Accepted
time: 201ms
memory: 7904kb
input:
897612484786617600 952878466220498188
output:
294147460
result:
ok 1 number(s): "294147460"
Test #25:
score: 0
Accepted
time: 129ms
memory: 7392kb
input:
970391875444992000 980657019550811949
output:
815016337
result:
ok 1 number(s): "815016337"
Test #26:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
1 1000000000000000000
output:
716070898
result:
ok 1 number(s): "716070898"
Test #27:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
916327 9000044616510005
output:
329767168
result:
ok 1 number(s): "329767168"
Test #28:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
7163391 998244353998244353
output:
438645041
result:
ok 1 number(s): "438645041"
Test #29:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
4033 4090
output:
392924428
result:
ok 1 number(s): "392924428"
Extra Test:
score: 0
Extra Test Passed