Codeforces round #259 (Div. 1) (A, B, C)

Source: Internet
Author: User
Codeforces round #259 (Div. 1)

Question A: the expectation for maximum I is (IN? (I? 1)N)?I/MN, Then the total expectation is ΣM1 (IN? (I? 1)N)?I/MNAnd then simplify the formula. The answer isM? ΣM? 11I/MN

Question B: To press DP, only the prime number smaller than 59 is used. There are 16 in total. DP [N] [s] indicates the nth number currently placed, the set used by prime numbers is the minimum value of S. S [k] indicates which prime numbers will be used for K numbers, and then performs state transfer.
DP (N, S) = dp (n-1, s ^ s [k]) + ABS (k-num [N])

Question C: DFS construction. In fact, the question prompts that no more than 4 n steps are allowed. In fact, if a tree is given, there is absolutely a way to construct it in this step, you can select a root node and use it as a tree. If you go through a path twice, it means you haven't actually gone. For each path, you only need to go through the path, go back to the root node. If the current location does not meet the requirements, take a further step. Then pay attention to the root node. In fact, the root node cannot be rolled back, but if the root node does not meet the requirements, so I just need to let the last step back to the root node not go, this search is complete, and it is determined that there are still nodes not satisfied, if there is, it must be because multiple trees cannot be constructed.

Code:

A:

#include <cstdio>#include <cstring>#include <cmath>int m, n;int main() {    scanf("%d%d", &m, &n);    double ans = 0;    for (int i = 1; i < m; i++)ans += pow((m - i) * 1.0 / m, n);    printf("%.12lf\n", m * 1.0 - ans);    return 0;}

B:

#include <cstdio>#include <cstring>#include <cstdlib>const int N = 105;const int MAXN = (1<<16) + 5;const int INF = 0x3f3f3f3f;int n, num[N], prime[N], pn = 0, vis[N], to[N];void get_prime() {    for (int i = 2; i < 59; i++) {if (vis[i]) continue;prime[pn++] = i;for (int j = i * i; j < 60; j += i) {    vis[j] = 1;}    }}int tra(int num) {    int ans = 0;    for (int i = 0; i < pn; i++) {if (num % prime[i] == 0) ans |= (1<<i);while (num % prime[i] == 0) num /= prime[i];    }    return ans;}int dp[N][MAXN], zh[N][MAXN][2];void print(int now, int s) {    if (now == 0) return;    print(now - 1, zh[now][s][0]);    printf("%d%c", zh[now][s][1], now == n? '\n' : ' ');}int main() {    get_prime();    scanf("%d", &n);    for (int i = 1; i <= n; i++)scanf("%d", &num[i]);    for (int i = 1; i < 59; i++)to[i] = tra(i);    int maxs = (1<<pn);    memset(dp, INF, sizeof(dp));    memset(dp[0], 0, sizeof(dp[0]));    for (int i = 1; i <= n; i++) {for (int j = 0; j < maxs; j++) {    for (int k = 1; k < 60; k++) {if ((j&to[k]) != to[k]) continue;int tmp = dp[i - 1][j^to[k]] + abs(k - num[i]);if (dp[i][j] > tmp) {    dp[i][j] = tmp;    zh[i][j][0] = (j^to[k]);    zh[i][j][1] = k;}    }}    }    int Min = INF, Min_v;    for (int i = 0; i < maxs; i++) {if (dp[n][i] < Min) {    dp[n][i] = Min;    Min_v = i;}    }    print(n, Min_v);    return 0;}

C:

#include <cstdio>#include <cstring>#include <vector>using namespace std;const int N = 100005;int n, m, x[N], now[N], vis[N], nv;vector<int> g[N], ans;void dfs(int u, int p) {    vis[u] = 1;    ans.push_back(u);    now[u] ^= 1;    for (int i = 0; i < g[u].size(); i++) {int v = g[u][i];if (vis[v]) continue;dfs(v, u);    }    if (x[u]^now[u]) {if (p != 0) {    ans.push_back(p);    ans.push_back(u);    now[p] ^= 1;    now[u] ^= 1;}    }    if (p != 0) {ans.push_back(p);now[p] ^= 1;    }}bool judge() {    if (x[nv]^now[nv]) {if (ans.size() == 0) return false;now[nv] ^= 1;ans.pop_back();    }    for (int i = 1; i <= n; i++)if (x[i]^now[i]) return false;    return true;}int main() {    scanf("%d%d", &n, &m);    int u, v;    while (m--) {scanf("%d%d", &u, &v);g[u].push_back(v);g[v].push_back(u);    }    for (int i = 1; i <= n; i++)scanf("%d", &x[i]);    memset(now, 0, sizeof(now));    for (int i = 1; i <= n; i++) {if (x[i]^now[i]) {    nv = i;    dfs(i, 0);    break;}    }    if (judge()) {int tmp = ans.size();printf("%d\n", tmp);for (int i = 0; i < tmp; i++) {    printf("%d%c", ans[i], i == ans.size() - 1 ? '\n' : ' ');}    }    else printf("-1\n");    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.