標籤:style color 使用 os io for ar 代碼
Codeforces Round #259 (Div. 1)
A題:最大值為i的期望為(in?(i?1)n)?i/mn,那麼總期望為∑m1(in?(i?1)n)?i/mn,然後化簡一下公式,答案為m?∑m?11i/mn
B題:狀壓DP,只需要用到小於59的素數,一共有16個,dp[n][s]表示當前放到第n個數字,素數使用的集合為s的最小值,S[k]表示k數字對應會用掉哪幾個素數,然後進行狀態轉移
dp(n, s) = dp(n - 1, s^S[k]) + abs(k - num[n])
C題:dfs構造,題目中其實給了提示,最多不超過4n步,那麼其實如果給定的是一棵樹,在這個步驟內是絕對有辦法構造出來的,可以選一個根結點出來,然後當成一棵樹,對於一條路徑,如果走兩遍,其實就等於沒走,那麼對於每條路徑,只需要走一遍走到底,再走回根結點,如果當前一個位置不滿足,就退一步在進一步,然後注意根結點,其實根結點是無法回退的,但是其實根結點如果不滿足,那麼我只要讓最後回到根結點的一步不走就可以了,這樣搜完,在判斷一下還有沒結點不滿足,如果有,肯定是多棵樹構造不出來的情況
代碼:
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;}