FJWC2020 Day4 problem solution

T1

Description

Known \ (A G_0 = \) , \ (G_1 = B \) , and has

\[ g_n=3g_{n-1}-g_{n-2}(n > 1) \]

Known \ (n-F_ {,} = 0 n-\) , and

\[ f_{n,k}=f_{g_n,k-1}(k>0) \]

Given (a, b, n, k , p \) \ value, you obtain \ (f_ {n, k} \ bmod p \) results.

\(0 ≤ a, b < p\)\(1 ≤ T ≤ 1000\)\(1 ≤ n, p ≤ 10^9\)\(1 ≤ k ≤ 100\)

Solution

First consider how beg \ (G_N g_ {} \) .

\ (g_n \) may be large, consider seeking \ (g_n \) loop section.

Note \ (F_n \) is the Fibonacci number of \ (n-\) key, play table can be found in \ (G_N = F_ {2N-F_ {B} × 2 (n--. 1)} × A \) .

So \ (g \) loop section with the same deed Fibonacci number sequence.

In particular, note the Fibonacci column mode \ (p \) minimum circulation within the meaning of section as \ (\ PI (the p-) \) .

If \ (P = \. 1 prod_ {I} = ^ {^ wp_i a_i} \) (decomposition of the quality factor), then:

\[\pi(p)=\text{lcm}_{i=1}^w(h(p_i)p_i^{a_i-1})\]

among them

\[h(p)= \begin{cases} 3& p=2\\ 20& p=5\\ p-1& p=±1\pmod5\\ 2p+2& p=±2\pmod5\\ \end{cases}\]

Proved too (I) complex (also) Miscellaneous (not) the (will), there will not start.

Time complexity \ (O (can live) \) .

Code

#include <bits/stdc++.h>

using namespace std;

#define ll long long

template <class t>
inline void read(t & res)
{
    char ch;
    while (ch = getchar(), !isdigit(ch));
    res = ch ^ 48;
    while (ch = getchar(), isdigit(ch))
    res = res * 10 + (ch ^ 48);
}

template <class t>
inline void print(t x)
{
    if (x > 9) print(x / 10);
    putchar(x % 10 + 48);
}

const int e = 1e7 + 5;

struct matrix
{
    int r, c;
    ll a[2][2];
};

ll now_p, A, B;
int L = 1e7, cnt, pri[e];
bool bo[e];

inline void init()
{
    int i, j;
    for (i = 2; i <= L; i++)
    {
        if (!bo[i]) pri[++cnt] = i;
        for (j = 1; j <= cnt && i * pri[j] <= cnt; j++)
        {
            bo[i * pri[j]] = 1;
            if (i % pri[j] == 0) break;
        }
    }
}

inline ll mul(ll x, ll y)
{
    ll res = x * y - (ll)((long double)x * y / now_p + 1e-8) * now_p;
    return res < 0 ? res + now_p : res; 
}

inline void add(ll &x, ll y)
{
    (x += y) >= now_p ? x -= now_p : 0; 
}

inline matrix operator * (matrix a, matrix b)
{
    matrix c;
    memset(c.a, 0, sizeof(c.a));
    int i, j, k;
    for (i = 0; i < a.r; i++)
    for (k = 0; k < b.r; k++)
    for (j = 0; j < b.c; j++)
    add(c.a[i][j], mul(a.a[i][k], b.a[k][j]));
    c.r = a.r; c.c = b.c;
    return c;
}

inline ll calc(ll n)
{
    matrix res, c;
    res.a[1][0] = res.a[1][1] = 0;
    res.a[0][0] = A % now_p; res.a[0][1] = B % now_p;
    res.r = 1; res.c = 2;
    c.a[0][0] = 0; c.a[0][1] = now_p - 1;
    c.a[1][0] = 1; c.a[1][1] = 3;
    c.r = c.c = 2;
    while (n)
    {
        if (n & 1) res = res * c;
        n >>= 1;
        c = c * c;  
    }
    return res.a[0][0];
}

inline ll get_g(ll p)
{
    return p == 2 ? 3 : p == 5 ? 20 : p % 5 == 1 ? p - 1 : p % 5 == 4 ? p - 1 : 2 * p + 2;
}

inline ll lcm(ll a, ll b)
{
    return a / __gcd(a, b) * b;
}

inline ll find(ll x)
{
    ll res = 1;
    int i;
    ll s = sqrt(x);
    for (i = 1; i <= cnt && pri[i] <= s; i++)
    if (x % pri[i] == 0)
    {
        int p = pri[i];
        ll t = get_g(p);
        while (x % p == 0) 
        {
            x /= p; t *= p;
        }
        t /= p;
        res = lcm(res, t);
    }
    if (x != 1) res = lcm(res, get_g(x));
    return res;
}

inline ll solve(ll n, ll k, ll p)
{
    if (!k) return n % p;
    ll cir = find(p), nxt = solve(n, k - 1, cir);
    now_p = p;
    ll res = calc(nxt);
    return res;
}

inline void work()
{
    read(A); read(B);
    int n, k, p;
    read(n); read(k); read(p);
    ll ans = solve(n, k, p);
    print(ans); putchar('\n');
}

int main()
{
    freopen("hakugai.in", "r", stdin);
    freopen("hakugai.out", "w", stdout);
    init();
    int T;
    read(T);
    while (T--) work();
    fclose(stdin);
    fclose(stdout);
    return 0;
}

T2

Description

Given a \ (n-\) tree points, points are numbered \ (0 \-n-SIM. 1 \) .

You can \ (k \) operations, each operation you can delete the current tree of any one side, then add an edge. You need to ensure that after each operation of the current map remains a tree, and each deletion after an edge can be added and deleted the same side edge.

Now you count, in \ (k \) after the operations, a total of how many trees may form will have.

Different forms two trees, if and only if there exists a side \ ((X, Y) \) , which appear in a tree, the tree does not appear in the other.

\ (1 \ leq n \ leq 50 \) , \ (0 \ leq k <n \) .

Solution

Title i.e. ask: How many trees meet up \ (K \) edges not the original tree.

Defines an edge \ ((u, v) \ ) weights: If \ ((u, v) \ ) in the original tree, a weight of \ (1 \) , otherwise \ (X \) .

We demand the right side is the product of \ (\ le x ^ k \ ) the number of spanning tree.

Is well known, the definition of the product is the right side of a spanning tree weight, the matrix values ​​can be determined tree Theorem weight and all spanning trees.

In this question, the sum of weights obtained are containing a \ (X \) a \ (n-1 \) order polynomial, \ (X ^ I \) coefficient is a weight of \ (I \) of the number of spanning tree, \ (the X-^ 0 \ the SIM the X-^ k \) coefficient sum is the answer.

I found not easy to find the polynomial directly, consider \ (\ text {lagrange} \ ) interpolation, i.e. so \ (X =. 1 \ n-SIM \) , a value obtained Theorem polynomial matrix tree.

The obtained values of all substitutes \ (\ text {lagrange} \ ) interpolation formula can restore the polynomial.

Time complexity \ (O (n-^. 4) \) .

Code

#include <bits/stdc++.h>

using namespace std;

#define ll long long

template <class t>
inline void read(t & res)
{
    char ch;
    while (ch = getchar(), !isdigit(ch));
    res = ch ^ 48;
    while (ch = getchar(), isdigit(ch))
    res = res * 10 + (ch ^ 48);
}

const int e = 155, mod = 998244353;

bool bo[e][e];
int a[e][e], inv[e], ans, n, m, c[e], d[e], h[e];

inline void add(int &x, int y)
{
    (x += y) >= mod && (x -= mod);
}

inline void del(int &x, int y)
{
    (x -= y) < 0 && (x += mod);
}

inline int mul(int x, int y)
{
    return (ll)x * y % mod;
}

inline int ksm(int x, int y)
{
    int res = 1;
    while (y)
    {
        if (y & 1) res = mul(res, x);
        y >>= 1;
        x = mul(x, x);
    }
    return res;
}

inline int gauss(int n)
{
    int i, j, k, res = 1;
    for (i = 1; i < n; i++)
    {
        int x = 0;
        for (j = i; j <= n; j++)
        if (a[j][i])
        {
            x = j;
            break;
        }
        if (!x) continue;
        if (x != i) res = mod - res, swap(a[x], a[i]);
        int inv = ksm(a[i][i], mod - 2);
        for (j = i + 1; j <= n; j++)
        {
            int v = mul(a[j][i], inv);
            for (k = i; k <= n; k++) del(a[j][k], mul(a[i][k], v));
        }
    }
    for (i = 1; i <= n; i++) res = mul(res, a[i][i]);
    return res;
}

inline int calc(int bas)
{
    int i, j;
    for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
    a[i][j] = 0;
    for (i = 1; i < n; i++)
    for (j = i + 1; j <= n; j++)
    {
        int v = 1;
        if (!bo[i][j]) v = bas;
        add(a[i][i], v); add(a[j][j], v);
        del(a[i][j], v); del(a[j][i], v);
    }
    return gauss(n - 1);
}

int main()
{
    freopen("kaisou.in", "r", stdin);
    freopen("kaisou.out", "w", stdout);
    read(n); read(m);
    int i, x, j, k;
    inv[1] = 1;
    for (i = 2; i <= n; i++)
    {
        read(x);
        x++;
        bo[x][i] = bo[i][x] = 1;
        inv[i] = mul(mod - mod / i, inv[mod % i]);
    }
    for (i = 1; i <= n; i++)
    {
        int y = calc(i);
        for (j = 1; j <= n; j++) c[j] = 0;
        c[0] = 1;
        for (j = 1; j <= n; j++)
        if (j != i)
        {
            if (i < j) y = mul(y, mod - inv[j - i]);
            else y = mul(y, inv[i - j]);
            for (k = 0; k <= n; k++)
            {
                h[k] = mul(mod - j, c[k]);
                if (k) add(h[k], c[k - 1]);
            }
            for (k = 0; k <= n; k++) c[k] = h[k];
        }
        for (j = 0; j <= n; j++) add(d[j], mul(c[j], y));
    } 
    for (i = 0; i <= m; i++) add(ans, d[i]);
    cout << ans << endl;
    fclose(stdin);
    fclose(stdout);
    return 0;
}

T3

Description

A tree given a weighted side, satisfied you obtain \ (U \) and \ (V \) edge weights of all edges in the shortest simple path \ (\ mathrm {gcd} \ ) of \ ( 1 \) points to \ ((u, v) ( u <v) \) number.

Not only that, you have to deal with \ (Q \) operating amendment right side after each modification, you have to answer to the output of the current situation.

\(2 ≤ n ≤ 10^5\)\(Q ≤ 100\)\(1 ≤ w, x ≤ 10^6\)

Solution

First, \ (ANS = \ sum_ {I =. 1} ^ {10 ^. 6} \ MU (I) F (I) \) , where \ (f (i) \) represents \ (\ mathrm {gcd} \ bmod i = 0 \) is the number of paths.

Subsequently, each edge \ ((u, v, w ) \) is split into \ ((U, V, W_1), (U, V, w_2), \ DOTS, (U, V, W_k) \) .

Wherein \ (w_1 \ sim w_k \) is \ (W \) all satisfy \ (\ mu \ ne 0 \ ) divisors.

So now \ (f (i) \) is the right of all sides to \ (i \) of the number of paths.

For each edge has been modified \ (X \) , respectively considered time \ (0 \ sim Q \) in, \ (X \) weights.

That this \ (Q + 1 \) weight time about all split into a number, and time marker on each edge.

Has not been modified for side \ (X \) , the time on the demolition of the edges are \ (- 1 \) , there is always expressed.

Next, the right side of the enumeration \ (v \) , calculate the right value of \ (v \) side of this \ (Q + 1 \) the contribution of answers.

Specifically, maintain a persistent disjoint-set, while maintaining a variable \ (SUM \) , said the current form how many sides are right \ (v \) path.

Enumeration time \ (T = -1 \ SIM Q \) .

First \ (t = -1 \) is added with disjoint-set, and then enumerate \ (\ lceil \) edge weight is absent \ (V \) edge \ (\ rfloor \) time \ (J \ ) , \ (ANS [J] = SUM + × \ MU (V) \) .

\ (t \ ge 0 \) when the time for each \ (T \) is added with disjoint-set, calculation of \ (ans [t] \) after contributions from all of these sides and then concentrated investigation delete.

Expected time complexity \ (O ((n-Q +) S \ log n-) \) , where \ (S \) of \ (10 ^ 6 \) number within the number average divisor.

Code

#include <bits/stdc++.h>

using namespace std;

#define ll long long

template <class t>
inline void read(t & res)
{
    char ch;
    while (ch = getchar(), !isdigit(ch));
    res = ch ^ 48;
    while (ch = getchar(), isdigit(ch))
    res = res * 10 + (ch ^ 48);
}

template <class t>
inline void print(t x)
{
    if (x > 9) print(x / 10);
    putchar(x % 10 + 48);
}

const int e = 2e5 + 5, o = 1e6 + 5;

struct point
{
    int id, t;
    
    point(){}
    point(int _id, int _t) :
        id(_id), t(_t) {}
};
struct line
{
    int x, y, z;
}b[e], c[e];
bool bo[o];
int fa[e], sze[e], n, q, miu[o], L = 1e6, tim[e], vis[e];
ll ans[e], sum;
vector<int>stk, a[o];
vector<point>g[o], h[e];

inline void init()
{
    int i, j;
    for (i = 1; i <= L; i++) miu[i] = 1;
    for (i = 2; i <= L; i++)
    if (!bo[i])
    {
        miu[i] = -1;
        a[i].push_back(i);
        for (j = i << 1; j <= L; j += i)
        {
            bo[j] = 1;
            if (j / i % i == 0) miu[j] = 0;
            else miu[j] = -miu[j];
            a[j].push_back(i);
        }
    }
}

inline int find(int x)
{
    while (fa[x]) x = fa[x];
    return x;
}

inline void merge(int x, int y)
{
    int fx = find(x), fy = find(y);
    if (sze[fx] < sze[fy]) swap(fx, fy);
    fa[fy] = fx;
    sum += (ll)sze[fx] * sze[fy];
    sze[fx] += sze[fy];
    stk.emplace_back(fy);
}

inline void cut()
{
    int fy = stk.back(), fx = fa[fy];
    sze[fx] -= sze[fy];
    sum -= (ll)sze[fx] * sze[fy];
    fa[fy] = 0;
    stk.pop_back();
}

inline void ins(int x, int id, int t)
{
    point u = point(id, t);
    int s, len = a[x].size(), i, all = 1 << len;
    for (s = 0; s < all; s++)
    {
        int y = 1;
        for (i = 0; i < len; i++)
        if (s & (1 << i)) y *= a[x][i];
        g[y].emplace_back(u);
    }
}

inline void solve(int x)
{
    int i, j, len = g[x].size(), k;
    sum = 0;
    ll tot = 0;
    for (i = 0; i < len; i = j + 1)
    {
        j = i - 1;
        int now_t = g[x][i].t;
        while (j < len - 1 && g[x][j + 1].t == now_t)
        {
            j++;
            int pos = g[x][j].id;
            merge(b[pos].x, b[pos].y);
        }
        ll tmp = miu[x] * sum;
        if (now_t != -1)
        {
            ans[now_t] += tmp;
            for (k = i; k <= j; k++) cut();
        }
        else
        {
            for (k = j + 1; k < len; k++) vis[g[x][k].t] = x;
            for (k = 0; k <= q; k++)
            if (vis[k] != x) ans[k] += tmp;
        }
    }
    while (stk.size()) cut();
}

int main()
{
    freopen("atoranta.in", "r", stdin);
    freopen("atoranta.out", "w", stdout);
    init();
    read(n);
    int i, j;
    for (i = 1; i < n; i++) read(b[i].x), read(b[i].y), read(b[i].z);
    read(q);
    for (i = 1; i <= q; i++) read(c[i].x), read(c[i].y), tim[c[i].x] = i;
    for (i = 1; i < n; i++)
    if (!tim[i]) ins(b[i].z, i, -1);
    for (i = 1; i <= q; i++)
    {
        point u = point(c[i].x, c[i].y);
        for (j = i; j <= q; j++)
        if (j == i || c[j].x != c[i].x) h[j].emplace_back(u);
        else break;
        bool pd = 0;
        for (j = 1; j < i; j++)
        if (c[j].x == c[i].x)
        {
            pd = 1;
            break;
        }
        if (!pd)
        {
            point v = point(c[i].x, b[c[i].x].z);
            for (j = 0; j < i; j++) h[j].emplace_back(v);
        }
    }
    for (i = 0; i <= q; i++)
    {
        int len = h[i].size();
        for (j = 0; j < len; j++) ins(h[i][j].t, h[i][j].id, i);
    }
    for (i = 1; i <= n; i++) sze[i] = 1;
    for (i = 1; i <= L; i++)
    if (g[i].size()) solve(i);
    for (i = 0; i <= q; i++) print(ans[i]), putchar('\n');
    fclose(stdin);
    fclose(stdout);
    return 0;
}

Guess you like

Origin www.cnblogs.com/cyf32768/p/12324132.html