2016 ACM / ICPC Asia Shenyang Station

 

[A. Thickest Burger]

Sign.

 

[B. Relative atomic mass]

Attendance

 

[C. Recursive sequence]

$$f[i] = f[i - 1] + 2 * f[i - 2] + i ^ 4$$

$$
\left[
\begin{matrix}
1 & 2 & 1 & 0 & 0 & 0 & 0 \\
1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 &0 & 1 & 4 & 6 &4 & 1 \\
0 & 0 & 0 & 1 & 3 & 3 & 1 \\
0 & 0 & 0 & 0 & 1 & 2 & 1 \\
0 & 0 & 0 & 0 & 0 & 1 & 1 \\
0 & 0 & 0 & 0 & 0 & 0 & 1
\end{matrix}
\right]
\times
\left[
\begin{matrix}
f_{i-1} & 0 & 0 & 0 & 0 & 0 & 0 \\
f_{i-2} & 0 & 0 & 0 & 0 & 0 & 0\\
i^4 &0 & 0 & 0& 0 &0 & 0 \\
i^3 & 0 & 0 & 0 & 0 & 0 & 0 \\
i^2 & 0 & 0 & 0 & 0 & 0 & 0\\
i & 0 & 0 & 0 & 0 & 0 & 0 \\
1 & 0 & 0 & 0 & 0 & 0 & 0
\end{matrix}
\right]=
\left[
\begin{matrix}
f_{i} & 0 & 0 & 0 & 0 & 0 & 0 \\
f_{i} & 0 & 0 & 0 & 0 & 0 & 0\\
(i+1)^4 &0 & 0 & 0& 0 &0 & 0 \\
(i+1)^3 & 0 & 0 & 0 & 0 & 0 & 0 \\
(i+1)^2 & 0 & 0 & 0 & 0 & 0 & 0\\
i+1 & 0 & 0 & 0 & 0 & 0 & 0 \\
1 & 0 & 0 & 0 & 0 & 0 & 0
\end{matrix}
\right]
$$

#include <bits/stdc++.h>
#define ll long long

const ll MOD = 2147493647;
const int N = 7;

struct Mat {
    ll a[10][10];
    Mat() {
        memset(a, 0, sizeof(a));
    }
    Mat(int x) {
        memset(a, 0, sizeof(a));
        for (int i = 1; i <= N; i++)
            a[i][i] = 1;
    }
    Mat operator * (const Mat &rhs) const {
        Mat c;
        for (int i = 1; i <= N; i++)
            for (int j = 1; j <= N; j++)
                for (int k = 1; k <= N; k++)
                    c.a[i][j] = (c.a[i][j] + a[i][k] * rhs.a[k][j] % MOD) % MOD;
        return c;
    }
};

Mat qp(Mat a, int b) {
    Mat c(1);
    while (b) {
        if (b & 1) c = a * c;
        a = a * a;
        b >>= 1;
    }
    return c;
}

int main() {
    int T;
    scanf("%d", &T);
    Mat base;
    base.a[1][1] = 1; base.a[1][2] = 2; base.a[1][3] = 1;
    base.a[2][1] = 1;
    base.a[3][3] = 1; base.a[3][4] = 4; base.a[3][5] = 6; base.a[3][6] = 4; base.a[3][7] = 1;
    base.a[4][4] = 1; base.a[4][5] = 3; base.a[4][6] = 3; base.a[4][7] = 1;
    base.a[5][5] = 1; base.a[5][6] = 2; base.a[5][7] = 1;
    base.a[6][6] = 1; base.a[6][7] = 1;
    base.a[7][7] = 1;
    while (T--) {
        int n, a, b;
        scanf("%d%d%d", &n, &a, &b);
        if (n == 1) {
            printf("%d\n", a);
            continue;
        }
        if (N == 2 ) { 
            printf ( " % d \ n " , b);
            continues ; 
        } 
        N - ; 
        n - ; 
        Mat years; 
        ans.a [ 1 ] [ 1 ] = b; ans.a [ 2 ] [ 1 ] = a; ans.a [ 3 ] [ 1 ] = 81 ; ans.a [ 4 ] [ 1 ] = 27 ; ans.a [ 5 ] [ 1 ] = 9 ; ans.a [ 6 ] [ 1 ] = 3 ; ans.a [ 7 ] [ 1 ] = 1 ; 
        years = qp ( base , n) * years; 
        printf ( " % lld \ n " , ans.a [ 1 ] [ 1 ]% MOD); 
    } 
    Return  0 ; 
}
View Code

 

[E. Counting Cliques]

After the baptism of the summer off more cattle school, saw group thought the storm to search ...

First with the bfs + bitset. T a.

Into a vector sentence, and T a.

I saw a solution to a problem is dfs, store groups is an array.

I put the code into a share of the array vector, or T.

STL card so do ...

#include <bits/stdc++.h>

const int N = 107;
std::vector<int> G[N];
bool mp[N][N];

int ans, n, m, s;

void solve(int *a, int u) {
    if (a[0] == s) {
        ans++;
        return;
    }
    for (int v: G[u]) {
        if (v <= u) continue;
        bool flag = 1;
        for (int j = 1; j <= a[0]; j++) {
            if (!mp[a[j]][v]) {
                flag = 0;
                break;
            }
        }
        if (!flag) continue;
        a[++a[0]] = v;
        solve(a, v);
        --a[0];
    }
}

int a[N];

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d%d", &n, &m, &s);
        for (int i = 0; i <= n; i++) {
            G[i].clear();
            for (int j = 0; j <= n; j++)
                mp[i][j] = 0;
            a[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            if (u > v) std::swap(u, v);
            G[u].push_back(v);
            mp[u][v] = mp[v][u] = 1;
        }
        ans = 0;
        for (int i = 1; i <= n; i++) {
            a[0] = 1;
            a[1] = i;
            solve(a, i);
        }
        printf("%d\n", ans);
    }
    return 0;
}
View Code

 

[H. Guessing the Dice Roll]

AC first automatic machine built out. DP on the AC automaton.

$dp[i] = \sum \frac{1}{6} \times dp[from]$

From $ $ $ I $ is not able to go to the end of a string of nodes

Because there will be a ring, the Gaussian elimination on the line.

#include <bits/stdc++.h>

const int N = 100 + 7;

double mat[N][N];
const double eps = 1e-8;

inline void gauss(int n) {
    for(int i = 0; i <= n; i++) {
        int r = i;
        for(int j = i + 1; j <= n; j++)
            if(std::fabs(mat[r][i]) < std::fabs(mat[j][i])) 
                r = j;
        if(r != i) std::swap(mat[i], mat[r]);
        for(int j = 0; j <= n; j++) {
            if(j == i) continue;
            double t = mat[j][i] / mat[i][i];
            for(int k = i; k <= n + 1; k++)
                mat[j][k] -= mat[i][k] * t;
        }
    }
    for(int i = 1; i <= n; i++) {
        mat[i][n + 1] /= mat[i][i];
    }
} 

struct Aho {
    static const int sz = 6;
    int ch[N][sz], last[N], fail[N], tol;
    bool end[N];
    void init() {
        tol = 0;
        newnode();
    }
    inline int newnode() {
        memset(ch[tol], 0, sizeof(ch[tol]));
        last[tol] = fail[tol] = end[tol] = 0;
        return tol++;
    }
    void insert(int *a, int n) {
        int u = 0;
        for (int i = 0; i < n; i++) {
            int id = a[i] - 1;
            if (!ch[u][id]) ch[u][id] = newnode();
            u = ch[u][id];
        }
        end[u] = 1;
    }
    void build() {
        std::queue<int> que;
        for (int i = 0; i < sz; i++)
            if (ch[0][i]) que.push(ch[0][i]), fail[ch[0][i]] = last[ch[0][i]] = 0;
        while (!que.empty()) {
            int u = que.front(); que.pop();
            end[u] |= end[last[u]];
            for (int i = 0; i < sz; i++) {
                int &v = ch[u][i];
                if (v) {
                    fail[v] = ch[fail[u]][i];
                    que.push(v);
                    last[v] = end[fail[v]] ? fail[v] : last[fail[v]];
                } else {
                    v = ch[fail[u]][i];
                }
            }
        }
    }
    void solve() {
        memset(mat, 0, sizeof(mat));
        mat[0][tol] = -1.0;
        for (int i = 0; i < tol; i++) {
            mat[i][i] = -1.0;
            if (end[i]) continue;
            for (int j = 0; j < sz; j++)
                mat[ch[i][j]][i] += 1.0 / 6;
        }
        gauss(tol - 1);
        bool flag = 0;
        for (int i = 0; i < tol; i++)
            if (end[i]) {
                if (flag) putchar(' ');
                printf("%.6f", mat[i][tol]);
                flag = 1;
            }
        puts("");
    }
} ac;

int a[20];

int main() {
    int T;
    scanf("%d", &T);
    for (; T--; ) {
        int n, l;
        scanf("%d%d", &n, &l);
        ac.init();
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < l; j++)
                scanf("%d", a + j);
            ac.insert(a, l);
        }
        ac.build();
        ac.solve();
    }
    return 0;
}
View Code

 

[I. The Elder]

$dp[u] = min(dp[anc] + (sum[u] - sum[anc])^2 + p)$

ANC is $ $ $ U nodes on the path to the root $.

DP slope optimize it. Upon entering a storage node at the current modification queue, change it back when leaving a node, so that we can ensure that when entering a node, stored in the queue are its ancestors.

#include <bits/stdc++.h>
#define pii pair<int, int>
#define ll long long
#define fi first
#define se second

const int N = 1e5 + 7;
const double eps = 1e-10;
ll dp[N];
int n, p, que[N], l, r;
ll sum[N];
std::vector<std::pii> G[N];

inline ll sqr(ll x) {
    return x * x;
}

inline ll up(int i, int j) {
    return dp[i] + sqr(sum[i]) - dp[j] - sqr(sum[j]);
}

inline ll down(int i, int j) {
    return sum[i] - sum[j];
}

ll ans;

void dfs(int u, int fa = 0) {
    std::vector<std::pii> vec;
    int x = l, y = r;
    while (l < r && up(que[l + 1], que[l]) <= down(que[l + 1], que[l]) * 2 * sum[u]) {
        vec.push_back(std::pii(l, que[l]));
        l++;
    }
    if (u != 1) {
        dp[u] = dp[que[l]] + sqr(sum[u] - sum[que[l]]) + p;
        ans = std::max(ans, dp[u]);
    }
    while (l < r && up(que[r], que[r - 1]) * down(u, que[r]) >= up(u, que[r]) * down(que[r], que[r - 1])) {
        vec.push_back(std::pii(r, que[r]));
        r--;
    }
    que[++r] = u;
    for (auto v: G[u]) {
        if (v.fi == fa) continue;
        sum[v.fi] = sum[u] + v.se;
        dfs(v.fi, u);
    }
    l = x, r = y;
    for (auto p: vec)
        que[p.fi] = p.se;
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &n, &p);
        for (int i = 1; i <= n; i++) 
            dp[i] = sum[i] = 0, G[i].clear();
        for (int i = 1, u, v, w; i < n; i++) {
            scanf("%d%d%d", &u, &v, &w);
            G[u].push_back(std::pii(v, w));
            G[v].push_back(std::pii(u, w));
        }
        dp[1] = -p;
        que[l = r = 1] = 0;
        ans = 0;
        dfs(1);
        printf("%lld\n", ans);
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Mrzdtz220/p/11689609.html