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

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;
}

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

时间: 2024-10-27 17:02:47

Codeforces Round #259 (Div. 1) (A,B,C)的相关文章

Codeforces Round #259 (Div. 2) 解题报告

终于重上DIV1了.... A:在正方形中输出一个菱形 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月01日 星期五 23时27分55秒 4 5 #include<vector> 6 #include<set> 7 #include<deque> 8 #include<stack> 9 #include<bitset> 10 #inclu

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine

Codeforces Round #259 (Div. 2) (序列)

题目链接:http://codeforces.com/contest/454/problem/B B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a se

Codeforces Round #259 (Div. 2)

A. Little Pony and Crystal Mine 题意:输出一个类似于十字形的东西 题解:直接打印即可... 代码: 1 var n,i,j:longint; 2 begin 3 readln(n); 4 for i:=1 to n>>1 do 5 begin 6 for j:=1 to (n-i<<1+1)>>1 do write('*'); 7 for j:=1 to i<<1-1 do write('D'); 8 for j:=1 to

Codeforces Round #259 (Div. 2)-D. Little Pony and Harmony Chest

题目范围给的很小,所以有状压的方向. 我们是构造出一个数列,且数列中每两个数的最大公约数为1; 给的A[I]<=30,这是一个突破点. 可以发现B[I]中的数不会很大,要不然就不满足,所以B[I]<=60左右.构造DP方程DP[I][J]=MIN(DP[I][J],DP[I][J^C[K]]+abs(a[i]-k)); K为我们假设把这个数填进数组中的数.同时开相同空间记录位置,方便输出结果.. #include<iostream> #include<stdio.h>

Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(模拟)

题目链接:Codeforces Round #259 (Div. 2)  B. Little Pony and Sort by Shift 求给出的序列最少移动多少次成为非下降序列.移动方式:只能将最后一个元素移到第一个位置 即:a1,?a2,?...,?an?→?an,?a1,?a2,?...,?an?-?1. 从后前开始搜非下降的子序列,然后前面的子序列接在其后面,最后判断变化后的序列是否是非下降序列即可. AC代码: #include<stdio.h> #include<strin

Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)

题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是所有最大值是xi的情况数/总情况数一共是m^n种,掷n次,所有最大值是xi的情况数应该是xi^n,但是这里边却包含着最大值非xi且不超过xi的种数,所以再减去最大值是xi-1或者最大值不超过这个的情况数.即sum += xi * (xi^n-(xi-1)^n)/m^n,但是这样求肯定是不行,因为m

Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum

题目链接 题意: 分析: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio> 6 #include <vector> 7 #include <algorithm> 8 #define LL long long 9 using namespace std; 10 11 in

Codeforces Round #259 (Div. 1)——Little Pony and Expected Maximum

题目连接 题意: 输入n.m,表示一个n面的色子(面上的值为1-n),投掷m次,求得到的最大值的期望(1?≤?m,?n?≤?105). 分析: 假设当前得到的最大值是Max,那么对应的概率是:sigma(C(m,k) * ((1 / n) ^ k )*(((Max - 1) / n) ^ (m - k)) ),(1 <= k <= n):化简就可以得到:sigma(C(m,k) * ((1 / n) ^ k )*(((Max - 1) / n) ^ (m - k)) ) - ((Max - 1