2019ICPC西安 C. Dirichlet k-th root

我不会狄利克雷卷积。输得彻底。

给出一个很直观的解法代码。

前提是理解好 f(n) = xf^k(n) = kx+b_{k,n}

那么

f^{2k}(n) = (f^k * f^k)(n) = 2f^k(n) + \sum_{d|n, d\not=1,d\not=n} f^k(d) f^k(n/d)

f^{k+1}(n) = (f^k * f)(n) = f^k(n) + f(n) + \sum_{d|n, d\not=1,d\not=n} f(d) f^k(n/d)

首先显然 f^k(1) = 1,假设已经求出 f(1) \cdots f(n-1) 里的所有值,以及可能取到的上标 k,那么就可以得到 g(n) = f^K(n) = Kx+b_{K,n}。我们可以从小往大递推 b_{i,n},然后求得 b_{K,n} 后得到 f(n),并解出 f^k(n) 以方便后续计算。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
const int mod = 998244353;
int g[maxn], f[maxn][64], b[maxn][64];
vector<int> d[maxn];
int n, k, kk[64], invk, tok;

int fpow(long long a, int k) {
    long long b = 1;
    for (; k; k >>= 1, a = a * a % mod)
        if (k & 1) b = b * a % mod;
    return b;
}

int main()
{
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", &g[i]);
    for (int i = 2; i <= n; i++)
        for (int j = i+i; j <= n; j += i)
            d[j].push_back(i);
    for (int tmp = k; tmp; ) {
        kk[++tok] = tmp;
        if (tmp % 2 == 1) tmp--; else tmp /= 2;
    } // the used ks
    invk = fpow(k, mod-2);

    // find for each f(n)
    for (int i = 1; i <= tok; i++) f[1][i] = 1;
    for (int i = 2; i <= n; i++)
    {
        // if we know about f[1..n-1], let f[n] = x
        // so it can be proved that fk[n] = kx+b
        for (int j = tok-1; j; j--)
        {
            long long tmp = b[i][j+1];
            if (!(kk[j] & 1)) tmp <<= 1;
            int sec = (kk[j] & 1) ? tok : j+1;
            for (auto dd : d[i])
                tmp += 1ll * f[dd][j+1] * f[i/dd][sec] % mod;
            b[i][j] = tmp % mod;
        }

        // now we have g[n] = Kx+b, just...
        f[i][tok] = 1ll * (g[i] - b[i][1] + mod) * invk % mod;
        for (int j = tok-1; j; j--)
            f[i][j] = (1ll * kk[j] * f[i][tok] + b[i][j]) % mod;
        assert(f[i][1] == g[i]);
    }

    for (int i = 1; i <= n; i++)
        printf("%d ", f[i][tok]);
    return 0;
}

复杂度为 O(n\log n \log k)

另外给出一个道听途说的做法,当时完全没有想到的。

如果你手算能力够强,那么可以发现 b 是个类似于求和的东西,结果对 k 敏感,如果令 k = 998244353,那么求和结果为 0,对 g\operatorname{inv}k 次也可以得到 f

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
const int mod = 998244353;
int g[maxn], f[maxn], n;

int fpow(long long a, int k) {
    long long b = 1;
    for (; k; k >>= 1, a = a * a % mod)
        if (k & 1) b = b * a % mod;
    return b;
}

void conv(int a[maxn], const int b[maxn]) {
    static int c[maxn];
    fill(c, c+n+1, 0);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j * i <= n; j++)
            c[j*i] = (c[j*i] + 1ll * a[i] * b[j]) % mod;
    copy(c, c+n+1, a);
}

int main()
{
    int k; scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", &g[i]);
    k = fpow(k, mod-2); f[1] = 1;
    for (; k; k >>= 1, conv(g, g))
        if (k & 1) conv(f, g);
    for (int i = 1; i <= n; i++)
        printf("%d ", f[i]);
    return 0;
}

不太会证明,能AC,复杂度为 O(n\log n \log \operatorname{inv} k)

2018ICPC北京 C. Pythagorean triple

数出所有满足 a^2+b^2=c^2c \le n 的正整数三元组。

首先,如果 (x,y,z) 是一个勾股数三元组,那么 (ax,ay,az) 也是个勾股数三元组。那么,不妨考虑 f(n) 为斜边长为 n 的勾股三元组数量,g(n) 为斜边长为 n 的本原勾股数,那么有

F(n) = \sum_{i=1}^n f(i) = \sum_{i=1}^n \sum_{d|i} g(d) = \sum_{d=1}^n \sum_{i=1}^{\lfloor n/d \rfloor} g(i) = \sum_{d=1}^n G(\left\lfloor\frac{n}{d}\right\rfloor)

考虑到所有本原勾股三元组可以表示为 (i^2-j^2, 2ij, i^2+j^2),其中 \gcd(i,j)=1,并且不能同时为奇数,那么有

\begin{aligned} G(n) & = \sum_{i 为奇} \sum_{j 为偶} [i^2+j^2 \le n] [\gcd(i,j)=1] \\ & = \sum_{d=1}^{\sqrt{n/2}} \mu(d) \sum_{i 为奇} \sum_{j 为偶} [i^2+j^2 \le n] [d|i] [d|j] \\ & = \frac{1}{2} \sum_{d=1}^{\sqrt{n/2}} \mu(d) \left( \sum_{i} \sum_{j} – \sum_{i 为奇} \sum_{j 为奇}\right ) [i^2+j^2 \le n] [d|i] [d|j] \\ & = \frac{1}{2} \sum_{d=1}^{\sqrt{n/2}} \mu(d) \left( \sum_{i} \left\lfloor\frac{\sqrt{n-i^2d^2}}{d}\right\rfloor – [d为奇] \sum_{i 为奇} \left\lfloor\frac{\frac{\sqrt{n-i^2d^2}}{d}+1}{2}\right\rfloor \right ) \end{aligned}

感觉写到这里一脸复杂度会超掉的样子,那么继续化简试试。

\begin{aligned} F(n) & = \sum_{c=1}^n G(\left\lfloor\frac{n}{c}\right\rfloor) \\ & = \frac{1}{2} \sum_{c=1}^n \sum_{d=1}^{\sqrt{n/2c}} \mu(d) \left( \sum_{i} \left\lfloor\frac{\sqrt{n/c-i^2d^2}}{d}\right\rfloor – [d为奇] \sum_{i 为奇} \left\lfloor\frac{\frac{\sqrt{n/c-i^2d^2}}{d}+1}{2}\right\rfloor \right ) \\ & = \frac{1}{2} \sum_{d=1}^{\sqrt{n/2}} \mu(d) \sum_{c=1}^n \left( \sum_{i} \left\lfloor\sqrt{\frac{n}{cd^2}-i^2}\right\rfloor – [d为奇] \sum_{i 为奇} \left\lfloor\frac{1}{2}\left(\sqrt{\frac{n}{cd^2}-i^2}+1\right)\right\rfloor \right ) \end{aligned}

那么可以发现最后那个对 c 求和的东西对外只和 n/d^2 有关系,对内可以数论分块,一脸时间复杂度 O(n^{3/4}) 的模样,复杂度够了,加个记忆化。冲冲冲~

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;

inline int sqrtd(int x)
{
    int tot = sqrtl(x);
    while (tot * tot < x) tot++;
    while (tot * tot > x) tot--;
    return tot;
}

inline pair<lld,lld> c(int n)
{
    static unordered_map<int,pair<lld,lld>> _C;
    if (_C.count(n)) return _C[n];
    lld p1 = 0, p2 = 0; int m = sqrtd(n);

    for (int i = 1, j = m; i <= m; i++)
    {
        while (i * i + j * j > n) j--;
        p1 += j;
        if (i & 1) p2 += (j+1)/2;
    }

    return _C[n] = make_pair(p1, p2);
}

inline lld h(int n, bool odd)
{
    lld ans = 0;

    for (int L = 1, R; L <= n; L = R+1)
    {
        R = n / ( n / L );
        auto cur = c(n/L);
        ans += (R-L+1) * cur.first;
        if (odd) ans -= (R-L+1) * cur.second;
    }

    return ans;
}

int main()
{
    const int MAXN = 1e6+5;
    static int pri[MAXN], pcn;
    static char isnp[MAXN], miu[MAXN];
    miu[1] = 1;

    for (int i = 2; i < MAXN; i++)
    {
        if (!isnp[i]) pri[pcn++] = i, miu[i] = -1;
        for (int j = 0; j < pcn && i * pri[j] < MAXN; j++)
        {
            isnp[i * pri[j]] = 1;
            if (i % pri[j] == 0) break;
            miu[i * pri[j]] = -miu[i];
        }
    }

    int T, n;
    scanf("%d", &T);

    while (T--)
    {
        scanf("%d", &n);
        lld ans = 0;
        for (int d = 1; d * d <= n; d++)
            if (miu[d]) ans += miu[d] * h(n/d/d, d&1);
        printf("%lld\n", ans/2);
    }

    return 0;
}

2019CCPC秦皇岛 H. Houraisan Kaguya

题目链接

首先,如果你的群论足够好,你应该能理解到,

f(a,b) = \frac{\operatorname{ord}a}{\gcd(\operatorname{ord}a,\operatorname{ord}b)}

所以题目实际上是求

\sum_{i=1}^n \sum_{j=1}^n \frac{\operatorname{ord}a_i \times \operatorname{ord}a_j}{\gcd(\operatorname{ord}a_i,\operatorname{ord}a_j)^2}

然后现场赛到这里我就不会了(雾)

实际上,我们可以按莫比乌斯反演的套路想到,枚举 gcd。我们考虑到 \operatorname{ord}x 也一定是 p-1 的倍数,所以考虑这样的一个卷积

\begin{aligned} c_k &= \sum_{\gcd(i, j) = k} ijf_if_j \\ &=\sum_{i,j} if_i ~ jf_j [\gcd(i,j)=k] \end{aligned}

其中 f_i 表示 \operatorname{ord} a_x = i 的个数,那么考虑枚举 \gcd 的倍数,则有

\begin{aligned} {c’}_k &= \sum_{x}c_x[k|x] \\ &= \sum_{i,j} if_i ~ jf_j [k|\gcd(i,j)] \\ &= \sum_{i,j} if_i ~ jf_j [k|i][k|j] \\ &= \sum_{i} if_i [k|i] \sum_{j} jf_j [k|j] \end{aligned}

也就是需要作一个类似于

{c’}_k = \sum_{x} c_x[k|x]

的正变换和逆变换。考虑到 FWT 中那个类似于按位与卷积的东西,设计一个类似于高位前缀和的东西,就可以做了。

另外求 \operatorname{ord}x 也有一点注意。朴素的想法是先将 x 拉入模 p 非零元素乘法群的一个 {p_i}^{e_i} 阶子群中,也就是先设计 x_i = x^{\frac{p-1}{{p_i}^{e_i}}},然后求 x_i 在该子群中的周期。所以可以设计一个时间复杂度为 O(c(p-1) \log(p-1)) 的算法。在此过程中,计算的瓶颈在于 x_i 的计算,耗费了绝大部分的时间复杂度,那么我们可以设计一个分治的算法,将因子集合划分为两部分,前半部分将后半部分素因子子群的周期升阶到 1,后半部分同样操作前半部分,然后继续分治。类似于多项式多点求值的一个想法,时间复杂度降为 O(\log c(p-1) \log(p-1))

目前这份代码是CF上跑的最快的,756ms,用普通求阶时间为 1575ms。

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
typedef long double flt;
const int MAXN = 2e5+5, S = 10;
int n, w, fc[20], dcn;
lld mod, fp[20], zs[20];

inline lld mul(lld x, lld y, lld z) { lld t = flt(x) * y / z; lld ans = (x * y - t * z) % z; if (ans < 0) ans += z; return ans; }
inline lld mul(lld x, lld y) { lld t = flt(x) * y / mod; lld ans = (x * y - t * mod) % mod; if (ans < 0) ans += mod; return ans; }
inline void addeq(lld &x, lld y) { x = x+y - (x+y>=mod?mod:0); }
inline void muleq(lld &x, lld y) { lld t = flt(x) * y / mod; lld ans = (x * y - t * mod) % mod; if (ans < 0) ans += mod; x = ans; }
inline lld fpow(lld x, lld n, lld mod) { x %= mod; if (n == 1) return x; lld ret = 1; for (; n; n>>=1, x=mul(x,x,mod)) if (n&1) ret=mul(ret,x,mod); return ret; }
inline lld fpow(lld x, lld n) { x %= mod; if (n == 1) return x; lld ret = 1; for (; n; n>>=1, x=mul(x,x,mod)) if (n&1) ret=mul(ret,x,mod); return ret; }

namespace Decompose {
    int tol; lld factor[1000];

    bool millerRabin(lld n, lld base) {
        lld n2 = n-1, s = __builtin_ctzll(n2); n2 >>= s;
        lld t = fpow(base, n2, n);
        if (t == 1 || t == n-1) return true;
        for (s--; s >= 0; s--)
            if ((t=mul(t,t,n))==n-1)
                return true;
        return false;
    }

    bool isPrime(lld n) {
        static lld bases[12] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };
        if (n <= 2) return false;
        for (int i = 0; i < 12 && bases[i] < n; i++)
            if (!millerRabin(n, bases[i])) return false;
        return true;
    }

    inline lld f(lld x, lld mod) {
        lld ans = 1; addeq(ans, mul(x, x, mod)); return ans;
    }

    void PollardRho(lld n) {
        if (n == 1) {
            return;
        } else if (isPrime(n)) {
            factor[tol++] = n;
        } else if (n & 1) {
            for (int i = 1; ; i++) {
                lld x = i, y = f(x, n), q = __gcd(y-x, n);
                while (q == 1) {
                    x = f(x, n), y = f(f(y, n), n);
                    q = __gcd((y-x+n)%n, n) % n;
                }
                if (q != 0 && q != n) {
                    PollardRho(q), PollardRho(n/q);
                    return;
                }
            }
        } else {
            while (!(n & 1)) factor[tol++] = 2, n >>= 1;
            PollardRho(n);
        }
    }

    inline void findfac(lld n) {
        tol = w = 0; PollardRho(n); map<lld,int> tj;
        for (int i = 0; i < tol; i++) tj[factor[i]]++;
        for (auto ss : tj) {
            fc[w] = ss.second, fp[w] = ss.first;
            zs[w] = 1; for (int i = 0; i < fc[w]; i++) zs[w] *= fp[w];
            w++;
        }
    }
}

using Decompose::findfac;

lld getOrd(lld a) {
    lld res = mod-1;
    for (int i = 0; i < w; i++) {
        lld qwq = fpow(a, res/=zs[i]);
        while (qwq != 1) qwq = fpow(qwq, fp[i]), res *= fp[i];
    }
    return res;
} // Time complexity: O(c(n)log(n))

struct Node { int l, r; lld val; };

lld getOrdFast(lld a) {
    static Node Q[100000]; int front = 0, rear = 0;
    lld res = 1; Q[rear++] = Node { 0, w-1, a };
    while (front < rear) {
        Node f = Q[front++];
        if (f.l == f.r) {
            for (lld bs = f.val; bs != 1; bs = fpow(bs, fp[f.l]))
                res *= fp[f.l];
        } else if (f.l < f.r) {
            int mid = (f.l+f.r)>>1;
            lld prod1 = 1, prod2 = 1;
            for (int i = f.l; i <= mid; i++) prod2 *= zs[i];
            for (int i = mid+1; i <= f.r; i++) prod1 *= zs[i];
            Q[rear++] = Node { f.l, mid, fpow(f.val, prod1) };
            Q[rear++] = Node { mid+1, f.r, fpow(f.val, prod2) };
        }
    }
    return res;
} // Time complexity: O(log(c(n))log(n))

lld di[MAXN], invdi2[MAXN]; int qp[20];
lld a[MAXN], g[MAXN];

void genFac(int wi, lld fac, int ids) {
    if (wi == w) {
        di[ids] = fac;
        invdi2[ids] = fac==1?1:mod-mod/fac;
        muleq(invdi2[ids], invdi2[ids]);
    } else {
        lld pr = 1;
        for (int i = 0; i <= fc[wi]; i++, pr *= fp[wi])
            genFac(wi+1, fac*pr, ids+i*qp[wi]);
    }
}

void addResult(lld ord)
{
    int id = 0; lld n = ord;

    for (int i = 0; i < w; i++)
    {
        int r = 0;
        while (n % fp[i] == 0) n /= fp[i], r++;
        id += r * qp[i];
    }

    addeq(g[id], ord);
}

void solve()
{
    findfac(mod - 1);

    qp[0] = 1;
    for (int i = 1; i < w; i++)
        qp[i] = qp[i-1] * (fc[i-1] + 1);
    dcn = qp[w-1] * (fc[w-1] + 1);
    genFac(0, 1, 0);

    for (int i = 1; i <= n; i++)
        addResult(getOrdFast(a[i]));
    for (int i = 0; i < w; i++)
        for (int j = dcn-1; j >= 0; j--)
            if (j / qp[i] % (fc[i]+1) != fc[i])
                addeq(g[j], g[j+qp[i]]);
    for (int i = 0; i < dcn; i++)
        muleq(g[i], g[i]);
    for (int i = 0; i < w; i++)
        for (int j = 0; j < dcn; j++)
            if (j / qp[i] % (fc[i]+1) != fc[i])
                addeq(g[j], mod-g[j+qp[i]]);
    lld ans = 0;
    for (int i = 0; i < dcn; i++)
        addeq(ans, mul(g[i], invdi2[i]));
    printf("%lld\n", ans);
}

int main()
{
    srand(time(NULL));
    scanf("%d %lld", &n, &mod);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
    solve();
    return 0;
}

2019 ICPC上海网络赛部分题解

C. Triple

考虑分情况不同做法。当 n \le 1000 时,枚举 A_i, B_j,则根据不等式解出满足条件的 C_k \in [|A_i-B_j|,A_i+B_j]。当 n > 1000,考虑枚举不合法的方案数,即形如 A_i + B_j < C_k 的方案数,不等式左边可以通过FFT取得,然后前缀和再枚举 C_k 容斥掉即可。

大力施加常数优化,AC仅需1256ms啦啦啦~

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
typedef complex<double> cplx;
int revs[1<<19], A[300010]; cplx wmks[1<<19];
const double PI = acos(-1.0);

void dft(cplx a[], int DFT, int N) {
    int *rev = revs + N;
    for (int i = 0; i < N; i++)
        if (i < revs[N+i]) swap(a[i], a[rev[i]]);
    for (int m = 2, m2 = 1; m <= N; m <<= 1, m2 <<= 1) {
        cplx *wmk = wmks + m + (DFT==-1 ? m2 : 0), u, t;
        for (int k = 0; k < N; k += m)
            for (int j = 0; j < m2; j++)
                t = wmk[j] * a[k+j+m2], u = a[k+j],
                a[k+j] = u + t, a[k+j+m2] = u - t;
    }
    if (DFT == -1) for (int i = 0; i < N; i++) a[i] /= N;
}

int a[100010], b[100010], c[100010];

lld solveWithFFT(int n)
{
    static cplx f[262144], g[262144], h[262144];
    static lld bc[262144], ac[262144], ab[262144];
    int ma = 0, mb = 0, mc = 0, md, len = 1; cplx tmp, tmp2;
    for (int i = 0; i < n; i++) ma = max(ma, a[i]), mb = max(mb, b[i]), mc = max(mc, c[i]);
    md = max(ma+mb, max(mb+mc, mc+ma)) + 2; while (len < md) len <<= 1;
    for (int i = 0; i < len; i++) f[i] = g[i] = h[i] = 0;
    for (int i = 0; i < n; i++) f[a[i]] += 1, g[b[i]] += 1, h[c[i]] += 1;
    dft(f, 1, len), dft(g, 1, len), dft(h, 1, len);
    for (int i = 0; i < len; i++) tmp = f[i] * g[i], tmp2 = g[i] * h[i], g[i] = f[i] * h[i], f[i] = tmp2, h[i] = tmp;
    dft(f, -1, len), dft(g, -1, len), dft(h, -1, len);
    for (int i = 0; i < len; i++) bc[i] = f[i].real()+0.2, ac[i] = g[i].real()+0.2, ab[i] = h[i].real()+0.2;
    for (int i = 1; i < len; i++) bc[i] += bc[i-1], ac[i] += ac[i-1], ab[i] += ab[i-1];
    lld ans = 1LL * n * n * n;

    for (int i = 0; i < n; i++)
    {
        if (c[i] <= len) ans -= ab[c[i]-1];
        if (b[i] <= len) ans -= ac[b[i]-1];
        if (a[i] <= len) ans -= bc[a[i]-1];
    }

    return ans;
}

inline int abs(int x) { return x < 0 ? -x : x; }

lld solveWithBruteForce(int n)
{
    for (int i = 0; i <= 200001; i++) A[i] = 0;
    for (int i = 0; i < n; i++) A[c[i]]++;
    for (int i = 1; i <= 200001; i++) A[i] += A[i-1];
    lld ans = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            ans += A[a[i]+b[j]] - A[max(abs(a[i]-b[j])-1,0)];
    return ans;
}

void solve(int cas)
{
    int n; scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    for (int i = 0; i < n; i++) scanf("%d", &b[i]);
    for (int i = 0; i < n; i++) scanf("%d", &c[i]);
    printf("Case #%d: %lld\n", cas, n <= 1000 ? solveWithBruteForce(n) : solveWithFFT(n));
}

int main()
{
    for (int N = 2, N2 = 1; N <= (1<<18); N <<= 1, N2 <<= 1)
    {
        int *rev = revs + N; cplx *wmk = wmks + N;
        for (int i = 0; i < N; i++)
            rev[i] = (rev[i>>1]>>1)|((i&1)?N2:0);
        for (int i = 0; i < N2; i++)
            wmk[i] = cplx(cos(PI*i/N2), sin(PI*i/N2));
        for (int i = 0; i < N2; i++)
            wmk[i+N2] = cplx(wmk[i].real(), -wmk[i].imag());
    }

    int T; scanf("%d", &T);
    for (int i = 1; i <= T; i++) solve(i);
    return 0;
}

D. Counting Sequences I

首先发现是几个本质不同的方案的排列,于是想到打表。需要一个快速出结果的剪枝。

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MOD = 1e9+7;
int n, jspx[3010], val[3010];
lld fac[3010], inv[3010], invs[3010], ans;

void dfs(int cur, int last, lld mul, int sum)
{
    if (cur == n)
    {
        if (mul == sum)
        {
            for (int i = 1; i <= n; i++)
                if (jspx[i])
                    printf("%dx%d ", jspx[i], i);
            printf("\n");
            lld tot = fac[n];
            for (int i = 1; i <= n; i++)
                tot = tot * invs[jspx[i]] % MOD;
            ans = (ans + tot) % MOD;
        }
    }
    else if (mul * pow(last, n-cur) <= sum + last * (n - cur))
    {
        for (int i = last; i <= n; i++)
        {
            val[cur] = i;
            jspx[i]++;
            dfs(cur + 1, i, mul * i, sum + i);
            jspx[i]--;
        }
    }
}

int main()
{
    fac[0] = fac[1] = invs[0] = invs[1] = inv[1] = 1;
    for (int i = 2; i < 3010; i++)
        fac[i] = i * fac[i-1] % MOD,
        inv[i] = (MOD-MOD/i) * inv[MOD%i] % MOD,
        invs[i] = invs[i-1] * inv[i] % MOD;
    scanf("%d", &n);
    dfs(0, 1, 1, 0);
    printf("ANS = %lld\n", ans);
    return 0;
}

E. Counting Sequences II

不知道题解在写什么鬼东西……

考虑 [1,m] 中的每个数字 t 的排列型生成函数。如果 t 为奇数,则其生成函数为 f(x) = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \cdots = e^x;否则只能出现偶数次,则为 g(x) = 1 + \frac{x^2}{2!} + \frac{x^4}{4!} + \cdots = \frac{e^x + e^{-x}}{2} = \ch(x)

那么答案相当于求

\left(\frac{e^x+e^{-x}}{2}\right)^{\left\lfloor \frac{m}{2} \right\rfloor} \left(e^x\right)^{\left\lceil \frac{m}{2} \right\rceil}

\frac{x^n}{n!} 项系数。注意到 \left\lceil \frac{m}{2} \right\rceil = \left\lfloor \frac{m}{2} \right\rfloor + (m \mod 2),令 k = \left\lfloor \frac{m}{2} \right\rfloor,则可以化简为

\frac{1}{2^k} \sum_{s=0}^k C_{k}^{s} e^{(2s+(m\mod 2))x}

即最后答案为

\frac{1}{2^k} \sum_{s=0}^k \frac{k! (2s+(m\mod 2))^n}{s! (k-s)!}

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MOD = 1e9+7, MAXN = 2e5+5;
int fac[MAXN], inv[MAXN], invs[MAXN];

int fpow(lld a, lld k)
{
    lld b = 1; k %= MOD-1;
    for (; k; k >>= 1, a = a * a % MOD)
        if (k & 1) b = b * a % MOD;
    return b;
}

void solve()
{
    lld ans = 0, n; int m, k;
    scanf("%lld %d", &n, &m); k = m / 2;
    for (int s = 0; s <= k; s++)
        ans += 1ll * fac[k] * invs[s] % MOD * invs[k-s] % MOD * fpow(2*s+(m&1), n) % MOD;
    ans = ans % MOD * fpow((MOD+1)/2, k) % MOD;
    printf("%lld\n", ans);
}

int main()
{
    fac[0] = fac[1] = inv[1] = 1;
    invs[0] = invs[1] = 1;
    for (int i = 2; i < MAXN; i++)
        fac[i] = 1ll * i * fac[i-1] % MOD,
        inv[i] = 1ll * (MOD-MOD/i) * inv[MOD%i] % MOD,
        invs[i] = 1ll * invs[i-1] * inv[i] % MOD;
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

H. Luhhy’s Matrix

考虑让每个矩阵只被运算一次,那么可以使用类似于分块的想法设立分界线。

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MAXN = 2e5+5;
unsigned lastans, seed, pow17[16], pow19[16];
char cost[MAXN];

struct matrix
{
    char item[16][16];
    int col[16], row[16];
    // let row[k] = item[i][k], col[k] = item[k][i]

    void norm()
    {
        for (int k = 0; k < 16; k++)
        {
            row[k] = col[k] = 0;
            for (int i = 0; i < 16; i++)
                row[k] = (row[k] << 1) | (item[i][k] & 1),
                col[k] = (col[k] << 1) | (item[k][i] & 1);
        }
    }

    matrix operator*(const matrix &b) const
    {
        matrix ans;
        for (int i = 0; i < 16; i++)
            for (int j = 0; j < 16; j++)
                ans.item[i][j] = cost[col[i]&b.row[j]];
        ans.norm();
        return ans;
    }

    static matrix getOne()
    {
        matrix ans; memset(ans.item, 0, sizeof(ans.item));
        for (int i = 0; i < 16; i++) ans.item[i][i] = 1;
        ans.norm(); return ans;
    }

    unsigned getAns() const
    {
        unsigned ans = 0;
        for (int i = 0; i < 16; i++)
            for (int j = 0; j < 16; j++)
                ans += item[i][j] * pow17[i] * pow19[j];
        return ans;
    }

    void output() const
    {
        for (int i = 0; i < 16; i++)
            for (int j = 0; j < 16; j++)
                printf("%d%c", int(item[i][j]), " \n"[j==15]);
    }

    static matrix generate()
    {
        matrix ans;
        seed ^= lastans;
        for (int i = 0; i < 16; i++)
        {
            seed ^= seed * seed + 15;
            for (int j = 0; j < 16; j++)
                ans.item[i][j] = (seed >> j) & 1;
        }
        ans.norm();
        return ans;
    }
} Q[MAXN], tail;

void solve()
{
    int front = 0, rear = 0, cut = 0, n; lastans = 0;
    scanf("%d", &n); tail = matrix::getOne();

    while (n--)
    {
        int t; scanf("%d %u", &t, &seed);

        if (t == 1)
        {
            Q[rear++] = matrix::generate();
            if (rear == front+1) tail = matrix::getOne(), cut = rear;
            else tail = Q[rear-1] * tail;
        }
        else
        {
            front++;

            if (front == cut)
            {
                cut = rear;
                for (int i = rear-2; i >= front; i--) Q[i] = Q[i+1] * Q[i];
                tail = matrix::getOne();
            }
        }

        lastans = (front == rear) ? 0 : (tail * Q[front]).getAns();
        printf("%u\n", lastans);
    }
}

int main()
{
    for (int sz2 = 1; sz2 < 65536; sz2 <<= 1)
        for (int i = 0; i < sz2; i++)
            cost[sz2+i] = cost[i] ^ 1;
    pow17[0] = pow19[0] = 1;
    for (int i = 1; i < 16; i++)
        pow17[i] = pow17[i-1] * 17,
        pow19[i] = pow19[i-1] * 19;
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

K. Peekaboo

圆上整点。直接分解 a,b 然后暴力枚举点对。看题解以为卡了64倍常数,结果交了就过了(大雾)

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
typedef pair<int,int> pear;
#define F first
#define S second
inline lld sqr(int a) { return 1ll * a * a; }
lld gcd(lld a, lld b) { return b ? gcd(b, a%b) : a; }

void gaussInteger(lld R, vector<pear> &tot)
{
    auto check = [&tot] (lld r, lld rr) -> void
    {
        if (r == 1 || r % 4 != 1) return;
        for (int n = 1; n*n*2 < r; n++)
        {
            int m = int(sqrt(r-n*n)+1e-5);
            if (m * m + n * n != r) continue;
            if (gcd(m,n) != 1 || m <= n) continue;
            tot.emplace_back(rr*(m*m-n*n), 2*n*m*rr);
        }
    };

    for (int i = 1; i * i <= R; i++)
    {
        if (R % i) continue;
        check(R/i, i);
        if (i * i != R) check(i, R/i);
    }
}

void fillOver(vector<pear> &proc, int r)
{
    int cnt = proc.size();
    proc.resize(cnt*8+4);
    for (int i = 0; i < cnt; i++)
        proc[i+cnt] = pear(proc[i].S, proc[i].F);
    for (int i = 0; i < 2*cnt; i++)
        proc[i+cnt*2] = pear(proc[i].F, -proc[i].S),
        proc[i+cnt*4] = pear(-proc[i].F, proc[i].S),
        proc[i+cnt*6] = pear(-proc[i].F, -proc[i].S);
    proc[cnt*8] = pear(0, r);
    proc[cnt*8+1] = pear(r, 0);
    proc[cnt*8+2] = pear(0, -r);
    proc[cnt*8+3] = pear(-r, 0);
}

pair<pear,pear> ans[100050];

void solve()
{
    vector<pear> possA, possB;
    int a, b, c, tot = 0;
    scanf("%d %d %d", &a, &b, &c);
    gaussInteger(a, possA), gaussInteger(b, possB);
    fillOver(possA, a), fillOver(possB, b);

    for (auto A : possA) for (auto B : possB)
    {
        lld r2 = sqr(c), r3 = sqr(A.F-B.F)+sqr(A.S-B.S);
        if (r2 == r3) ans[tot++] = make_pair(A, B);
    }

    sort(ans, ans+tot);
    printf("%d\n", tot);
    for (int i = 0; i < tot; i++) printf("%d %d %d %d\n", ans[i].F.F, ans[i].F.S, ans[i].S.F, ans[i].S.S);
}

int main()
{
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

HDU6414 带劲的多项式

考虑形如

f(x) = \prod_{i=1}^r (x-\lambda_i)^{l_i}

形式的多项式,它的一阶导函数为

f'(x) = \left( \sum_{i=1}^{r}l_i(x-\lambda_i) \right) \left(\prod_{i=1}^r (x-\lambda_i)^{l_i -1}\right)

则可以发现它们之间有

\gcd(f(x), f'(x)) = \prod_{i=1}^r (x-\lambda_i)^{l_i-1}

则有

\frac{f(x)}{\gcd(f(x), f'(x))} = \prod_{i=1}^r (x-\lambda_i)

由于根重数不同,每个根的因子会在不同时刻从上式消失。每次将 \gcd(f, f’) 当作新的 f 进行迭代即可。

另外,STL好慢啊233。

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MOD = 998244353;
typedef vector<int> Poly;

inline lld fpow(lld a, int k)
{
    lld b = 1;
    for (; k; k>>=1, a=a*a%MOD)
        if (k&1) b=b*a%MOD;
    return b;
}

void norm(Poly &a)
{
    lld qwq = fpow(a.back(), MOD-2);
    for (auto &x : a) x = x * qwq % MOD;
}

Poly diff(const Poly &a)
{
    Poly _a = Poly(a.size() - 1);
    for (int i = 0; i < _a.size(); i++)
        _a[i] = a[i+1] * (i+1LL) % MOD;
    return _a;
}

void divide(const Poly &a, const Poly &p, Poly &q, Poly &r)
{
    assert(p.size() >= 1 && p.back() != 0);
    q.clear(), r = a; int P = p.size();
    lld pq = fpow(p.back(), MOD-2);

    for (int i = a.size()-1; i >= P-1; i--)
    {
        q.push_back(r[i] * pq % MOD);
        for (int j = 0; j < P; j++)
            r[i-P+1+j] = (r[i-P+1+j] + MOD - 1LL * r[i] * pq % MOD * p[j] % MOD) % MOD;
    }

    reverse(q.begin(), q.end());
    while (q.back() == 0) q.pop_back();
    while (r.back() == 0) r.pop_back();
}

Poly gcd(Poly a, Poly b)
{
    Poly c, d;
    while (b.size()) divide(a, b, c, d), a = b, b = d;
    norm(a);
    return a;
}

void solve()
{
    int n; scanf("%d", &n); vector<pair<int,int>> ans;
    Poly orig(n+1, 0), dif, cbs, tmp, nil, nul;
    for (int i = 0; i <= n; i++) scanf("%d", &orig[i]);
    norm(orig); dif = diff(orig), tmp = gcd(orig, dif);
    divide(orig, tmp, cbs, nil); orig = tmp, norm(orig);

    for (int l = 1; dif.size(); l++)
    {
        dif = diff(orig), tmp = gcd(orig, dif);
        divide(orig, tmp, nul, nil);

        if (nul != cbs)
        {
            // an root appears!
            Poly a, b;
            divide(cbs, nul, b, a);
            assert(b.size() == 2);
            ans.emplace_back((MOD-b[0])%MOD, l);
            cbs = nul;
        }

        orig = tmp, norm(orig);
    }

    printf("%d\n", int(ans.size()));
    for (auto p : ans) printf("%d %d\n", p.first, p.second);
}

int main()
{
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

2019 ICPC南昌网络赛部分题解

A. Enju with Math Problem

给定欧拉函数表中连续100项,求是否存在或从哪个点开始。

考虑和去年EC筛 \mu^2(n) 一样的思路,检查 5, 7, 9, 11, 13, 17, 23 的倍数所在位置,利用中国剩余定理求解可能位置,然后利用区间筛筛出该区间的欧拉函数,对比。

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MAXN = 2e4+5;
int pri[MAXN], pcn; bool isnp[MAXN];
int test[100], qaq[100], phi[100];

void init_prime()
{
    for (int i = 2; i < MAXN; i++)
    {
        if (!isnp[i]) pri[pcn++] = i;
        for (int j = 0; j < pcn; j++)
        {
            if (i * pri[j] >= MAXN) break;
            isnp[i * pri[j]] = true;
            if (i % pri[j] == 0) break;
        }
    }
}

int check(int y)
{
    phi[0] = qaq[0] = y;
    for (int i = 1; i < 100; i++)
        phi[i] = qaq[i] = qaq[i-1] + 1;

    int stt = qaq[0];
    if (stt <= 0 || qaq[99] > 150000000) return -1;

    for (int i = 0; i < pcn; i++)
    {
        int ft = ((stt - 1) / pri[i] + 1) * pri[i] - stt;
        for (int j = ft; j < 100; j += pri[i])
        {
            phi[j] = phi[j] / pri[i] * (pri[i] - 1);
            while (qaq[j] % pri[i] == 0) qaq[j] /= pri[i];
        }
    }

    for (int i = 0; i < 100; i++)
        if (qaq[i] > 1)
            phi[i] = phi[i] / qaq[i] * (qaq[i] - 1);
    for (int i = 0; i < 100; i++)
        if (test[i] != phi[i])
            return -1;
    return stt;
}

lld extgcd(lld a, lld b, lld &x, lld &y)
{
    if (!b) return x = 1, y = 0, a;
    lld d = extgcd(b, a % b, y, x);
    return y -= (a / b) * x, d;
}

pair<lld,lld> modeqs(lld b[], const int w[], int k)
{
    lld bj = b[0], wj = w[0], x, y, d;
    for (int i = 1; i < k; i++)
    {
        b[i] %= w[i]; d = extgcd(wj, w[i], x, y);
        if ((bj - b[i]) % d != 0) return make_pair(0LL, -1LL);
        x = x * (b[i] - bj) / d % (w[i] / d), y = wj / d * w[i];
        bj = ((bj + x * wj) % y + y) % y, wj = y;
    }
    return make_pair(bj, wj);
}

int bs[8][25]; lld b[8];
const int m[8] = { 6, 4, 6, 10, 12, 16, 18, 22 };
const int W[8] = { 9, 5, 7, 11, 13, 17, 19, 23 };
vector<int> poss[8];

int dfs(int i)
{
    if (i == 8)
    {
        auto getAns = modeqs(b, W, 8);
        if (getAns.first == 0) return -1;
        return check(getAns.first);
    }
    else
    {
        for (auto v : poss[i])
        {
            b[i] = W[i] - v;
            int t = dfs(i+1);
            if (t > 0) return t;
        }

        return -1;
    }
}

int solve()
{
    memset(bs, 0, sizeof(bs));
    for (int i = 0; i < 8; i++)
        poss[i].clear();

    for (int i = 0; i < 100; i++)
    {
        scanf("%d", &test[i]);
        for (int j = 0; j < 8; j++)
            if (test[i] % m[j] == 0)
                bs[j][i % W[j]]++;
    }

    for (int i = 0; i < 8; i++)
        for (int j = 0; j < W[i]; j++)
            if (bs[i][j] >= 100 / W[i])
                poss[i].push_back(j);
    return dfs(0);
}

int main()
{
    init_prime();
    int T; scanf("%d", &T);

    while (T--)
    {
        int ans = solve();
        if (ans == -1) printf("NO\n");
        else printf("YES\n%d\n", ans);
    }

    return 0;
}

D. Interesting Series

考虑 F_n = 1 + a + a^2 + \cdots + a^{n-1} = \frac{a^n – 1}{a-1}

那么所求相当于是

Answer(K) = \frac{\sum_{s \in K} a^s – C_{N}^{|K|}}{a-1}

考虑构造多项式

f(n) = \prod_{i=1}^n (1 + a^s x)

那么所求 \sum_{s \in K} a^s 就是 f(n)x^{|K|} 项系数之和。

分治FFT计算即可。注意到模数比较小,所以考虑用double或者long double跑FFT,但是要注意预处理精度(感谢Claris姐姐呜呜呜),不能多次乘。

#include <bits/stdc++.h>
typedef long long lld;
const double PI = acos(-1.0);
const int MOD = 100003;
typedef std::complex<double> cplx;
int fac[MOD], inv[MOD], invs[MOD];

void dft(cplx a[], int DFT, int N) {
    static int rev[1<<20]; static cplx w[1<<20], ww[1<<20];
    for (int i = 0; i < N; i++) {
        rev[i] = (rev[i>>1]>>1)|((i&1)?N>>1:0);
        if (i < rev[i]) swap(a[i], a[rev[i]]);
        w[i] = cplx(cos(PI*i/N), sin(PI*i/N));
        ww[i] = cplx(w[i].real(), -w[i].imag());
    }
    for (int d = 0, ctz = __builtin_ctz(N); (1<<d) < N; d++) {
        cplx u, t; int m2 = 1<<d, m = m2<<1;
        for (int k = 0; k < N; k += m)
            for (int j = 0; j < m2; j++)
                t = (DFT==1?w:ww)[j<<(ctz-d)] * a[k+j+m2], u = a[k+j],
                a[k+j] = u + t, a[k+j+m2] = u - t;
    }
    if (DFT == -1) for (int i = 0; i < N; i++) a[i] /= N;
}

lld fpow(lld a, lld k) {
    lld b = 1;
    for (; k; k>>=1, a=a*a%MOD)
        if (k&1) b=b*a%MOD;
    return b;
}

int t[131072], ss[MOD], a;

void solve(int l, int r, int L, int R)
{
    if (R - L <= 31)
    {
        int len = R-L+1, sz = 1;
        for (int i = 0; i < len; i++) t[L+i] = !i;
        for (int w = l; w <= r; w++, sz++)
            for (int i = sz; i > 0; i--)
                (t[L+i] += t[L+i-1] * fpow(a, ss[w]) % MOD) %= MOD;
    }
    else
    {
        int mid = (L+R)>>1, m = (l+r)>>1, len=R-L+1;
        solve(l, m, L, mid), solve(m+1, r, mid+1, R);
        static cplx a[131072], b[131072];
        for (int i = 0; i < len/2; i++)
            a[i] = t[L+i], b[i] = t[mid+1+i],
            a[len/2+i] = b[len/2+i] = 0;
        dft(a, 1, len), dft(b, 1, len);
        for (int i = 0; i < len; i++) a[i] *= b[i];
        dft(a, -1, len);
        for (int i = 0; i < len; i++)
            t[L+i] = lld(a[i].real()+0.5) % MOD;
    }
}

int main()
{
    inv[0] = fac[0] = invs[0] = invs[1] = inv[1] = fac[1] = 1;
    for (int i = 2; i < MOD; i++)
        fac[i] = 1ll * fac[i-1] * i % MOD,
        inv[i] = 1ll * (MOD-MOD/i) * inv[MOD%i] % MOD,
        invs[i] = 1ll * invs[i-1] * inv[i] % MOD;

    int N, Q, s;
    scanf("%d %d %d", &N, &a, &Q);
    for (int i = 1; i <= N; i++)
        scanf("%d", &ss[i]);
    int sz = 30, st = 32;
    while (sz < N) sz <<= 1, st <<= 1;
    solve(1, N, 0, st-1);

    while (Q--)
    {
        scanf("%d", &s);
        lld ans = t[s] - 1ll * fac[N] * invs[s] * invs[N-s] % MOD;
        ans = (ans + MOD) * inv[a-1] % MOD;
        printf("%lld\n", ans);
    }

    return 0;
}

话说回来补题的时候一直没过竟然是因为……输出答案时那个组合数写错了呜呜呜,变量意义改变不改名,调试两行泪。

H. The Nth Item

考虑作出 f(n+x) = a \times f(n) + b \times f(n+1) 的表达式,然后分段打表。

#include <cstdio>
using namespace std;
typedef long long lld;
const int MAXN = 5e5+5;
const int MOD = 998244353;
int trans[1002][2], f[MAXN][2];

int main()
{
    trans[0][0] = trans[1][1] = f[0][1] = 1;

    for (int i = 2; i <= 1001; i++)
    {
        trans[i][0] = 2ll * trans[i-1][1] % MOD;
        trans[i][1] = (trans[i-1][0] + 3ll * trans[i-1][1]) % MOD;
    }

    for (int i = 1; i < MAXN; i++)
    {
        f[i][0] = (1ll * trans[1000][0] * f[i-1][0] + 1ll * trans[1000][1] * f[i-1][1]) % MOD;
        f[i][1] = (1ll * trans[1001][0] * f[i-1][0] + 1ll * trans[1001][1] * f[i-1][1]) % MOD;
    }

    int Q; lld N; scanf("%d %lld", &Q, &N);
    lld lastAns = N, tot = 0; N = 0;

    for (int i = 0; i < Q; i++)
    {
        lld now = lastAns ^ N; N = now; now %= (MOD-1)/2;
        lld ans = (1ll * trans[now%1000][0] * f[now/1000][0] + 1ll * trans[now%1000][1] * f[now/1000][1]) % MOD;
        tot ^= ans; lastAns = ans * ans;
    }

    printf("%lld\n", tot);
    return 0;
}

2019 ICPC徐州网络赛部分题解

E. XKC’s basketball team

就在区间 [i+1,R] 找最右边大于 a_i+m 的数的位置。然后考虑到线段树去查询,先查右子树,再查左子树,然后写着写着就成了二叉树了……

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5+5;
int mmax[MAXN*4], a[MAXN];
#define lson i<<1
#define rson i<<1|1
int n, m;

int query(int x, int y, int L, int R, int i)
{
    if (mmax[i] < x || R <= y) return -1; else if (L == R) return L;
    int mid = (L+R)>>1; int q = query(x, y, mid+1, R, rson);
    if (~q) return q; else return query(x, y, L, mid, lson);
}

void build(int L, int R, int i)
{
    if (L == R) { scanf("%d", &mmax[i]); a[L] = mmax[i]; return; }
    int mid = (L+R)>>1; build(L,mid,lson), build(mid+1,R,rson);
    mmax[i] = max(mmax[lson], mmax[rson]);
}

int main()
{
    scanf("%d %d", &n, &m);
    build(1, n, 1);
    for (int i = 1; i <= n; i++)
    {
        int loc = query(a[i]+m, i, 1, n, 1);
        if (loc > 0) loc = loc - i - 1;
        printf("%d%c", loc, " \n"[i==n]);
    }
    return 0;
}

H. function

想不到,也只能抄抄题解过日子了。

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MAXN = 2e5+5;
const int MOD = 998244353;
const int INV2 = (MOD+1)/2;
int pri[MAXN], pcn, sp[MAXN];
int m, Sqrt, id1[MAXN], id2[MAXN];
bool isnp[MAXN]; lld g[MAXN], h[MAXN], w[MAXN], n;

int main()
{
    scanf("%lld", &n);

    for (int i = 2; i < MAXN; i++)
    {
        if (!isnp[i])
        {
            pcn++; pri[pcn] = i;
            sp[pcn] = (sp[pcn-1] + i) % MOD;
        }

        for (int j = 1; j <= pcn; j++)
        {
            if (i * pri[j] >= MAXN) break;
            isnp[i * pri[j]] = true;
            if (i % pri[j] == 0) break;
        }
    }

    m = 0, Sqrt = sqrt(n);

    for (lld L = 1, R; L <= n; L = R+1)
    {
        R = n / (n / L), w[++m] = n / L;
        (w[m] <= Sqrt ? id1[w[m]] : id2[R]) = m;
        g[m] = (w[m] - 1) % MOD;
        h[m] = (w[m] % MOD + 2) * (w[m] % MOD - 1) % MOD * INV2 % MOD;
    }

    for (int j = 1; j <= pcn; j++)
    {
        for (int i = 1, d; i <= m && pri[j] <= w[i]/pri[j]; i++)
        {
            d = (w[i]/pri[j]<=Sqrt) ? id1[w[i]/pri[j]] : id2[n/(w[i]/pri[j])];
            g[i] -= g[d] - j + 1;
            h[i] -= pri[j] * (h[d] - sp[j-1]) % MOD;
        }
    }

    lld ans = 0;

    for (int i = 1; i <= pcn; i++)
    {
        if (n / pri[i] >= pri[i])
        {
            for (lld p = pri[i]; p <= n; p *= pri[i])
            {
                lld fk = n/p%MOD;
                ans += (n+1)%MOD*fk%MOD;
                ans -= fk*(fk+1)%MOD*INV2%MOD*p%MOD;
            }
        }
    }

    for (int i = 1; i < Sqrt; i++)
    {
        lld sp2 = h[i] - h[i+1], sp1 = g[i] - g[i+1];
        ans += (n+1)%MOD*i%MOD*sp1%MOD;
        ans -= i*(i+1ll)/2%MOD*sp2%MOD;
    }

    ans %= MOD; if (ans < 0) ans += MOD;
    printf("%lld\n", ans);
    return 0;
}

J. Random Access Iterator

假设深度为树高的叶节点集合为 R,则相当于从起点按某种移动方式转移到 R 中的概率。考虑 dp[u] 为从 u 转移不到 R 中的概率,那么有

dp[u] = \frac{1}{d(u)} \sum_{(u,v)\in T} dp[v]

同时注意深度为树高的叶节的概率为 0,其他叶节点概率为 1,即DP初值。然后两遍DFS解决。

#include <vector>
#include <cstdio>
using namespace std;
typedef long long lld;
const int MAXN = 1e6+5;
const int MOD = 1e9+7;
vector<int> G[MAXN];
int maxdep;

lld fpow(lld a, lld k)
{
    lld b = 1;
    for (; k; k>>=1, a=a*a%MOD)
        if (k&1) b=b*a%MOD;
    return b;
}

void dfs1(int u, int p, int d)
{
    maxdep = max(maxdep, d);
    for (auto v : G[u]) if (v != p) dfs1(v, u, d+1);
}

lld dfs2(int u, int p, int d)
{
    int k = 0; lld dp = 0;
    for (auto v : G[u]) if (v != p) dp += dfs2(v, u, d+1), k++;
    if (k) return fpow(dp * fpow(k, MOD-2) % MOD, k);
    else return d != maxdep ? 1 : 0;
}

int main()
{
    int n; scanf("%d", &n);

    for (int i = 1, u, v; i < n; i++)
    {
        scanf("%d %d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }

    dfs1(1,0,1);
    printf("%lld\n", (1+MOD-dfs2(1,0,1))%MOD);
    return 0;
}

K. Center

#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long lld;
typedef pair<int,int> pii;

const lld INF = 1e8;
const lld STD = 1e7;
#define X first
#define Y second

lld trans(int x, int y) { return x * INF + y; }
lld trans(pii n) { return n.X * INF + n.Y; }
pii inv(lld t) { return pii(t/INF, t%INF); }
pii operator*(pii a, int n) { return pii(a.X * n, a.Y * n); }
pii operator+(pii a, pii b) { return pii(a.X + b.X, a.Y + b.Y); }
pii operator-(pii a, pii b) { return pii(a.X - b.X, a.Y - b.Y); }

pii p[1010];
unordered_set<lld> existed;
unordered_map<lld, int> newCenter;

int main()
{
    int n, x, y, tot;
    scanf("%d", &n), tot = n;

    for (int i = 0; i < n; i++)
    {
        scanf("%d %d", &x, &y);
        x += STD, y += STD;
        p[i] = make_pair(x, y);
        existed.insert(trans(x, y));
    }

    for (int i = 0; i < n; i++)
    {
        int ans = 0;
        for (int j = 0; j < n; j++)
            if (!existed.count(trans(p[i] * 2 - p[j])))
                ans++;
        tot = min(tot, ans);
    }

    for (int i = 0; i < n; i++)
        for (int j = i+1; j < n; j++)
            newCenter[trans(p[i] + p[j])]++;

    int ptp = 0; pii nc;
    for (auto cnt : newCenter)
        if (cnt.second > ptp)
            nc = inv(cnt.first), ptp = cnt.second;
    int maybe = 0;
    for (int j = 0; j < n; j++)
        if (!existed.count(trans(nc - p[j])))
            maybe++;
    tot = min(tot, maybe);

    printf("%d\n", tot);
    return 0;
}

2019 ICPC南京网络赛部分题解

B. super_log

就考察一下你是不是真的理解了拓展欧拉定理。

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MAXN = 1e6+5;
int pri[MAXN], pcn, phi[MAXN];
bool isnp[MAXN];

lld fpow(lld a, lld k, lld m)
{
    lld b = 1;
    for (; k; k>>=1, a=a*a%m)
        if (k&1) b=b*a%m;
    return b;
}

bool judge(lld a, lld k, lld m)
{
    lld b = 1;
    while (k)
    {
        b = b * a;
        if (b >= m) return true;
        k--;
    }
    return false;
}

lld getAns(int a, int b, int m)
{
    if (m == 1) return a < m ? 0 : m;
    if (b == 0) return 1;
    lld t = getAns(a, b-1, phi[m]);
    lld ans = fpow(a, t, m);
    bool lg = t < phi[m] ? judge(a, t, m) : true;
    if (lg) ans += m;
    //printf("f(%d,%d,%d)=%lld\n", a, b, m, ans);
    return ans;
}

void solve()
{
    int a, b, m;
    scanf("%d %d %d", &a, &b, &m);
    printf("%lld\n", getAns(a,b,m)%m);
}

int main()
{
    phi[1] = 1;
    for (int i = 2; i < MAXN; i++)
    {
        if (!isnp[i]) pri[pcn++] = i, phi[i] = i-1;
        for (int j = 0; j < pcn; j++)
        {
            int k = i * pri[j];
            if (k >= MAXN) break;
            isnp[k] = true;
            if (i % pri[j] == 0) { phi[k] = phi[i] * pri[j]; break; }
            else phi[k] = phi[i] * (pri[j] - 1);
        }
    }

    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

C. Tsy’s number 5

抄题解。令 f_k = \sum_{i=1}^n [\varphi(i)=k],则有

\begin{aligned} ans & = \sum_{i=1}^n \sum_{j=1}^n \varphi(i) \varphi(j) 2^{\varphi(i) \varphi(j)} \\ & = \sum_{i=1}^n \sum_{j=1}^n f_i f_j ij 2^{ij} \\ & = 2 \sum_{i=1}^n \sum_{j=1}^i f_i f_j ij 2^{ij} – \sum_{i=1}^n {f_i}^2 i^2 2^{j^2} \\ & = 2 \sum_{i=1}^n f_ii \sum_{j=1}^i f_jj {\sqrt2}^{2ij} – \sum_{i=1}^n {f_i}^2 i^2 2^{j^2} \\ & = 2 \sum_{i=1}^n f_ii {\sqrt2}^{i^2} \sum_{j=1}^i f_jj {\sqrt2}^{j^2} {\sqrt2}^{-(i-j)^2} – \sum_{i=1}^n {f_i}^2 i^2 2^{j^2} \end{aligned}

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MAXN = 1e5+5;
const int MOD = 998244353, G = 3;
const int SQRT2 = 116195171;
int phi[MAXN], sp[MAXN], qwq[MAXN], A[262144], B[262144];

inline lld fpow(lld a, lld k)
{
    lld b = 1;
    for (; k; k>>=1, a=a*a%MOD)
        if (k & 1) b=b*a%MOD;
    return b;
}

inline void init_prime()
{
    static int pri[MAXN], pcn;
    static bool isnp[MAXN];

    phi[1] = sp[0] = 1; sp[1] = fpow(SQRT2, MOD-2);
    for (int i = 2; i < MAXN; i++)
    {
        if (!isnp[i]) pri[pcn++] = i, phi[i] = i-1;
        sp[i] = fpow(SQRT2, MOD-1-1ll*i*i%(MOD-1));
        for (int j = 0; j < pcn; j++)
        {
            if (i * pri[j] >= MAXN) break;
            isnp[i * pri[j]] = true;
            if (i % pri[j] == 0) { phi[i * pri[j]] = phi[i] * pri[j]; break; }
            else phi[i * pri[j]] = phi[i] * (pri[j] - 1);
        }
    }
}

inline void dft(int a[], int DFT, int N)
{
    static int rev[262144], wmk[262144];
    for (int i = 0; i < N; i++)
        rev[i] = (rev[i>>1]>>1)|((i&1)?(N>>1):0);
    for (int i = 0; i < N; i++)
        if (i < rev[i]) swap(a[i], a[rev[i]]);

    for (int m = 2, m2 = 1; m <= N; m <<= 1, m2 <<= 1) {
        wmk[0] = 1, wmk[1] = fpow(G, (MOD-1)/m*DFT+(DFT==-1?MOD-1:0));
        for (int j = 2; j < m2; j++) wmk[j] = 1ll * wmk[j-1] * wmk[1] % MOD;
        for (int k = 0; k < N; k += m)
            for (int j = 0, u, t; j < m2; j++)
                t = 1ll * wmk[j] * a[k+j+m2] % MOD, u = a[k+j],
                a[k+j] = (u+t)%MOD, a[k+j+m2] = (u-t+MOD)%MOD;
    }

    if (DFT == -1)
        for (int i = 0, invN = fpow(N, MOD-2); i < N; i++)
            a[i] = 1ll * a[i] * invN % MOD;
}

inline void solve()
{
    int n; scanf("%d", &n);
    int len = 1; while (len < n+1) len <<= 1; len <<= 1;
    for (int i = 0; i < len; i++) A[i] = B[i] = 0;
    for (int i = 0; i <= n; i++) B[i] = sp[i], A[phi[i]]++;
    for (int i = 0; i <= n; i++)
        qwq[i] = A[i] = 1ll * i * A[i] % MOD * fpow(SQRT2, 1ll*i*i%(MOD-1)) % MOD;
    dft(A, 1, len); dft(B, 1, len);
    for (int i = 0; i < len; i++) A[i] = 1ll * A[i] * B[i] % MOD;
    dft(A, -1, len); lld ans = 0;
    for (int i = 1; i <= n; i++)
        ans += (2ll * A[i] - qwq[i]) * qwq[i] % MOD;
    ans %= MOD; if (ans < 0) ans += MOD;
    printf("%lld\n", ans);
}

int main()
{
    init_prime();
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

D. Robots

考虑从 u 出发,到达 n 的期望步数,逆向建图拓扑排序搞定。

dp[u] = \frac{1}{d(u) + 1} \left( dp[u] + \sum_{(u,v)\in G} dp[v] \right) + 1

再考虑从 u 出发,到达 n 的期望消耗天数累计值,则有

dp2[u] = \frac{1}{d(u) + 1} \left( dp2[u] + \sum_{(u,v)\in G} dp2[v] \right) + dp[u]

其实是乱搞猜结论出来的,但是后面那个 dp[u] 的意义其实可以理解为,我这一步到终点的天数,是由路径上的每条边叠加得到的。

这个期望的期望好像题解没给证明,先这样吧QAQ。

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MAXN = 1e5+5;
vector<int> G[MAXN];
int d[MAXN], d2[MAXN], Q[MAXN];
double one[MAXN], dp[MAXN], dp2[MAXN];

void run(double dp[], double dist[], int n)
{
    int front = 0, rear = 0; Q[rear++] = n;
    memcpy(d, d2, sizeof(d));

    while (front < rear)
    {
        int u = Q[front++];
        if (u != n) dp[u] = (d2[u]+1.0)/d2[u]*dist[u] + dp[u]/d2[u];

        for (auto v : G[u])
        {
            d[v]--; if (d[v] == 0) Q[rear++] = v;
            dp[v] += dp[u];
        }
    }
}

void solve()
{
    int n, m; scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        dp2[i] = dp[i] = d2[i] = 0, G[i].clear(), one[i]=1;
    for (int i = 0, u, v; i < m; i++)
        scanf("%d %d", &u, &v), G[v].push_back(u), d2[u]++;
    run(dp, one, n);
    run(dp2, dp, n);
    printf("%.2lf\n", dp2[1]);
}

int main()
{
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

E. K Sum

套路变换。

\begin{aligned} f_n(k) & = \sum_{d=1}^n d^2 \left({\sum_{l_i=1}^n}\right)^k [\gcd(l_i) = 1] \\ & = \sum_{d=1}^n d^2 \sum_{i=1}^{\lfloor n/d \rfloor} \mu(i) \left\lfloor \frac{n}{id} \right\rfloor^k \\ & = \sum_{s=1}^n \left(\sum_{id=s}d^2 \mu(i)\right)\left\lfloor\frac{n}{s}\right\rfloor^k \end{aligned}

合并,后面那个等比数列求和,注意特判 1,所以需要得到 k \mod 1e9+7k \mod 1e9+6

\sum_{i=2}^k f_n(i) = \sum_{s=1}^n g(s) \sum_{i=2}^k\left\lfloor\frac{n}{s}\right\rfloor^i

杜教筛部分看起来像是 id^2 * \mu,那么配上 1

S(n) = \sum_{i=1}^n g(i) = \frac{n(n+1)(2n+1)}{6} – \sum_{i=2}^n S\left(\left\lfloor\frac{n}{i}\right\rfloor\right)

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
const int MOD = 1e9+7;
const int MAXN = 3e6+5;
int _G1[MAXN];

void init_prime()
{
    static int pri[MAXN], pcn;
    static bool isnp[MAXN];

    _G1[1] = 1;
    for (int i = 2; i < MAXN; i++)
    {
        if (!isnp[i]) pri[pcn++] = i, _G1[i] = (1ll * i * i-1) % MOD;
        for (int j = 0; j < pcn; j++)
        {
            if (i * pri[j] >= MAXN) break;
            isnp[i * pri[j]] = true;
            if (i % pri[j] == 0)
            {
                _G1[i * pri[j]] = _G1[i] * 1ll * pri[j] % MOD * pri[j] % MOD;
                break;
            }
            else
            {
                _G1[i * pri[j]] = _G1[i] * (1ll * pri[j] % MOD * pri[j] - 1)% MOD;
            }
        }
    }

    for (int i = 1; i < MAXN; i++)
        _G1[i] = (_G1[i-1] + _G1[i]) % MOD;
}

lld sumG(int n)
{
    static map<int,lld> _G2;
    if (n < MAXN) return _G1[n];
    if (_G2.count(n)) return _G2[n];

    lld ans = n * (n+1ll) % MOD * (2ll*n+1) % MOD * ((MOD+1)/6) % MOD;
    for (int L = 2, R; L <= n; L = R+1)
    {
        R = n / (n / L);
        ans -= (R-L+1) * sumG(n/L) % MOD;
    }

    ans %= MOD; if (ans < 0) ans += MOD;
    return _G2[n] = ans;
}

inline lld fpow(lld a, lld k)
{
    lld b = 1;
    for (; k; k>>=1, a=a*a%MOD)
        if (k&1) b=a*b%MOD;
    return b;
}

inline lld powers(int ns, int k, int k2)
{
    if (ns == 1) return k2-1;
    lld ans = fpow(ns, k+1) - 1ll * ns * ns % MOD;
    ans = ans * fpow(ns-1, MOD-2) % MOD;
    return ans;
}

inline void solve()
{
    int n, k = 0, k2 = 0; lld ans = 0;
    static char kk[MAXN];
    scanf("%d %s", &n, kk);
    for (int i = 0; kk[i]; i++)
        k = (k * 10ll + kk[i] - '0') % (MOD-1),
        k2 = (k2 * 10ll + kk[i] - '0') % MOD;

    for (int L = 1, R; L <= n; L = R+1)
    {
        R = n / (n / L);
        ans += (sumG(R) - sumG(L-1)) * powers(n/L, k, k2) % MOD;
    }

    ans %= MOD; if (ans < 0) ans += MOD;
    printf("%lld\n", ans);
}

int main()
{
    init_prime();
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

G. Quadrilateral

参考题解。我怎么写了一坨屎QAQ。

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
inline lld sum1(lld c) { return c*(c+1)/2; }
inline lld sum2(lld c) { return c*(c+1)*(2*c+1)/6; }

// count the pairs for [1,a] + [1,b] <= [1,c]
inline lld func1(lld a, lld b, lld c)
{
    if (a > b) swap(a, b); c--;
    if (a < 1 || b < 1) return 0;
    if (c <= 0) return 0;
    else if (c < a) return sum1(c)*(c+1) - sum2(c);
    else if (c <= b) return (c+1-a)*(c+1-a)*a - sum1(c-a)*a + sum1(a-1)*(c+1) - sum2(a-1);
    else if (c < a+b) return (c-b)*(a+b)*(c+1) - (a+b+c+1)*(sum1(c)-sum1(b)) + (sum2(c)-sum2(b)) + (b+1-a)*(c+1-a)*a - sum1(b-a)*a + sum1(a-1)*(c+1) - sum2(a-1);
    else return a*(a+b)*(c+1) - (a+b+c+1)*(sum1(a+b)-sum1(b)) + (sum2(a+b)-sum2(b)) + (b+1-a)*(c+1-a)*a - sum1(b-a)*a + sum1(a-1)*(c+1) - sum2(a-1);
}

// calc the valid pairs of ([1,a], [1,b], [1,c], d)
inline lld func2(lld a, lld b, lld c, lld d)
{
    return a * b * c
         - func1(b, c, a-d)
         - func1(a, c, b-d)
         - func1(a, b, c-d)
         - func1(a, b, d-1)
         + func1(a, b, d-c-1);
}

// calc the valid pairs of ([la,ra], [lb,rb], [lc,rc], d)
inline lld func3(pair<int,int> a, pair<int,int> b, pair<int,int> c, int d)
{
    return func2(a.second, b.second, c.second, d)
         - func2(a.second, b.first-1, c.second, d)
         - func2(a.first-1, b.second, c.second, d)
         - func2(a.second, b.second, c.first-1, d)
         + func2(a.first-1, b.first-1, c.second, d)
         + func2(a.first-1, b.second, c.first-1, d)
         + func2(a.second, b.first-1, c.first-1, d)
         - func2(a.first-1, b.first-1, c.first-1, d);
}

inline void solve()
{
    pair<int,int> iv[4]; lld ans = 0;
    for (int i = 0; i < 4; i++)
        scanf("%d %d", &iv[i].first, &iv[i].second);
    for (int i = iv[3].first; i <= iv[3].second; i++)
        ans += func3(iv[0], iv[1], iv[2], i);
    printf("%lld\n", ans);
}

int main()
{
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

2018CCPC吉林区域赛 G. High Priestess

题目链接:HDU-6561

给不超过 10^4 个阻值为 1 \Omega 的电阻,通过串联和并联使得最后形成的电路有效阻值为 r

这道题可以考虑有理逼近中的连分数。

a_0 + \dfrac{1}{a_1 + \dfrac{1}{a_2 + \dfrac{1}{\ddots + \dfrac{1}{a_n}}}}

我们通常将它记为 [a_0, a_1, a_2, \cdots, a_n]

\frac{p_k}{q_k}[a_0, a_1, \cdots, a_k],我们称 \frac{p_k}{q_k}[a_0, a_1, a_2, \cdots, a_n] 的第 k 个渐进分数。

可以证明,所有有理数都可以表示为这样的一个有限连分数。

考虑斐波那契的 \phi = \frac{1 + \sqrt5}{2},我们可以令连分数序列为无限的 [1, 1, 1, \cdots, 1, \cdots]。而且我们可以发现,\frac{p_k}{q_k} 正好等于 \frac{F_{k+1}}{F_k}。数论中还会研究,任何无理数也都可以用连分数来逼近。

可以看出来,将这样的一个有理数摆进来,首先将它的整数部分去掉,然后再求倒数,就变成了 [a_1, a_2, \cdots, a_n],问题规模得以缩小。当我们迭代到没有小数部分的时候,这个有理数已经被精确表示了。类似于一个辗转相除法的例子,对不对?时间复杂度为 O(\log q)

我们需要(cai)知(chu)道(lai)的是,渐进级数越精确,误差越小。如果我们用某个连分数来表示这道题的答案呢?

一定有 a_0 = 0。那么我们不妨设定一个数列 \lbrace b_i \rbrace,其中

b_i = \begin{cases} \frac{1}{a_i + \frac{1}{b_{i+1}}}, & i = 2k+1 \\ a_i + b_{i+1}, & i=2k \end{cases}

那么可以认为 b_i 是由某一些电阻构成的复合电阻。当 i 为奇数时,我们将 a_i1 \Omega 的电阻与 b_{i+1} 并联起来;当 i 为偶数时,我们将 a_i1 \Omega 的电阻与 b_{i+1} 串联起来。注意当超过连分数长度 n 时,b_{i+1}0\infty,取决于奇偶性。

首先题目给定的 r 输入进来以后一定是一个有理数。那我们不妨先求出 [ r – \epsilon, r + \epsilon ] 中分母最小的分数 \frac{p_i}{q_i}

我们将 \frac{p_i}{q_i} 的连分数表示出来。但是由于题目要求所用电阻数量不能超过 10^4,我们可以将超过的部分略去,留下一堆多余的电阻,这样比略去的逼近效果略好。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll EPS = 10000000;
const int MAXN = 10000;
int kase;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

void euclid(ll a, ll b, ll x, ll y, ll &p, ll &q)
{
    ll K = a / b; a -= K * b, x -= K * y;
    if (x > y) p = q = 1; else euclid(y, x, b, a, q, p);
    p += K * q;
}

void solve()
{
    static char buf[1000];
    static ll lfs[1000];
    scanf("%s", buf);
    ll fz = 0, fm = 1, fzm, fmm, fzM, fmM;

    for (int i = 2; buf[i]; i++)
    {
        fz = fz * 10 + buf[i] - '0';
        fm *= 10;
    }

    if (fm < EPS) fz *= EPS / fm, fm = EPS;
    fzm = fz - fm / EPS, fmm = gcd(fzm, fm), fzm /= fmm, fmm = fm / fmm;
    fzM = fz + fm / EPS, fmM = gcd(fzM, fm), fzM /= fmM, fmM = fm / fmM;
    euclid(fzm, fmm, fzM, fmM, fz, fm);
    if (fmM < fm) fz = fzM, fm = fmM;
    if (fmm < fm) fz = fzm, fm = fmm;
    int tot = 1, sum = 0, idx = 0, idg, id1, id2, idq = -1, op;

    while (fz && sum < MAXN)
    {
        swap(fz, fm);
        sum += (lfs[tot++] = min(fz / fm, MAXN - sum + 0ll));
        fz %= fm;
    }

    // printf("[ 0"); for (int i = 1; i < tot; i++) printf(", %lld", lfs[i]); printf(" ]\n");
    printf("Case %d:\n%d %d\n", ++kase, sum, sum-1);

    idg = sum-1;
    for (int j = tot-1; j > 0; j--)
    {
        op = j & 1;
        id1 = idx++;

        for (int i = 1; i < lfs[j]; i++)
        {
            id2 = idx++;
            printf("%d %d %d\n", op, id1, id2);
            id1 = ++idg;
        }

        if (~idq)
        {
            id2 = idq;
            printf("%d %d %d\n", op, id1, id2);
            ++idg;
        }

        idq = idg;
    }
}

int main()
{
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

这道题总而言之感觉还是有点怪怪的,例如 0.000001 一定是无解的,但是竟然也能 AC。就这样吧,应该是良心出题人没有出卡人数据。

2019杭电多校第五场 I. discrete logarithm problem

题目链接:HDU 6632

已知数字 a,b,p,并且 p=2^a3^b+1。想求解离散对数 a^x \equiv b \mod p

需要一个东西叫做 Pohlig-Hellman 算法。以下是来自维基百科内容的翻译,将使用群论语言。

p^e 阶循环群

这个算法用于计算 p^e 阶循环群 G=\langle g \rangle 的离散对数 g^x = h

  1. 初始化 x_0 := 0

  2. 计算 \gamma := g^{p^{e-1}},由拉格朗日配集分解计数定理知,ord~\gamma = p

  3. 对于 k \in \lbrace 0,\cdots,e-1 \rbrace,分别做

    1. 计算 h_k := (g^{-x_k}h)^{p^{e-1-k}}。由此构造可知,此元素的阶一定整除 p,即阶为 1p,因此 h_k \in \langle \gamma \rangle

    2. 使用 BSGS 算法,计算 d_k \in \lbrace 0, \cdots, p-1 \rbrace,其中 \gamma^{d_k} = h_k。这个操作的复杂度为 O(\sqrt{p})

    3. x_{k+1} := x_k + p^k d_k

  4. 返回 x_e

e \ll p 时,这个算法优于 BSGS,复杂度为 O(e\sqrt{p})

一般合数阶循环群

首先我们将 G = \langle g \rangle 的阶数刻画为 n = \prod_{i=1}^r {p_i}^{e_i}。现在我们解方程 g^x = h

  1. 对循环群阶数做因数分解

  2. 对于 i \in \lbrace 1, \cdots, r \rbrace,分别做

    1. 计算 g_i := g^{n / {p_i}^{e_i}},由拉格朗日定理知此元素的阶为 {p_i}^{e_i}

    2. 计算 h_i := h^{n/{p_i}^{e_i}},由此构造知 h_i \in \langle g_i \rangle

    3. 利用上面算法计算 x_i 使得 {g_i}^{x_i} = h_i

  3. 利用中国剩余定理解方程组 x \equiv x_i \mod {p_i}^{e_i}

  4. 返回 x

因此总的时间复杂度为

T(n) = O\left( \sum_{i=1}^r e_i (\log n + \sqrt{p_i}) \right)

回到本题

显然因为 p_i = 2, 3,我们不用 BSGS……好的。

#include <bits/stdc++.h>
using namespace std;
typedef long long lld;

lld fpow(lld _a, lld k, lld MOD) {
    __int128 b = 1, a = _a % MOD;
    while (k) {
        if (k & 1) b = b * a % MOD;
        a = a * a % MOD;
        k >>= 1;
    }
    return b;
}

__int128 extgcd(__int128 a, __int128 b, __int128 &x, __int128 &y) {
    if (!b) return x = 1, y = 0, a;
    __int128 d = extgcd(b, a % b, y, x);
    return y -= (a / b) * x, d;
}

int T, e2, e3, G;
lld MOD, n, p2, p3;

lld solvePE(lld g, lld h, lld p, lld pe, int e)
{
    lld x = 0, pp = 1;
    lld y = fpow(g, pe/p, MOD);

    for (int k = 0; k < e; k++)
    {
        __int128 hk = fpow(g, pe-x, MOD);
        hk = hk * h % MOD;
        hk = fpow(hk, pe/p/pp, MOD);
        __int128 d, yy;
        for (d = 0, yy = 1; d < p; d++, yy = yy * y % MOD)
            if (yy == hk) break;
        x = (x + pp * d) % MOD, pp *= p;
    }

    return x;
}

__int128 solveN(lld h)
{
    if (e3 == 0) return solvePE(G, h, 2, p2, e2);
    else if (e2 == 0) return solvePE(G, h, 3, p3, e3);
    __int128 x2 = solvePE(fpow(G, p3, MOD), fpow(h, p3, MOD), 2, p2, e2);
    __int128 x3 = solvePE(fpow(G, p2, MOD), fpow(h, p2, MOD), 3, p3, e3);
    __int128 invp2, invp3; assert(extgcd(p2, p3, invp2, invp3) == 1);
    return (x2 * p3 * invp3 + x3 * p2 * invp2) % (MOD-1);
}

int main()
{
    cin >> T;

    while (T--)
    {
        long long a, b;
        cin >> MOD >> a >> b;
        n = MOD-1, e2 = e3 = 0, p2 = p3 = 1;
        while (n % 2 == 0) n /= 2, p2 *= 2, e2++;
        while (n % 3 == 0) n /= 3, p3 *= 3, e3++;
        for (G = 2; (e2 > 0 && fpow(G, (MOD-1)/2, MOD) == 1)
                 || (e3 > 0 && fpow(G, (MOD-1)/3, MOD) == 1); G++);
        __int128 inda = solveN(a), indb = solveN(b), x, y;
        __int128 d = extgcd(inda, MOD-1, x, y);
        lld ans = indb % d ? -1
            : lld((indb / d * x % (MOD-1) + (MOD-1)) % ((MOD-1)/d));
        cout << ans << endl;
    }

    return 0;
}

我竟然都忘了 extgcd 是怎么求多解的,我好菜啊QAQ