codeforces.com/contest/839/

来源:互联网 发布:js如何显示表单内容 编辑:程序博客网 时间:2024/06/08 00:11

http://codeforces.com/contest/839/

A

#include<stdio.h>#include<math.h>#include<string.h>#include<queue>#include<vector>#include<map>#include<algorithm>using namespace std;const int INF = 0x3f3f3f3f;int main(){    int n, k, y = 0;    scanf("%d%d", &n, &k);    int ans = -1;    for(int i = 0; i < n; ++i) {        int x;        scanf("%d", &x);        y += x;        if(y > 8) k -= 8, y -= 8;        else k -= y, y = 0;        if(k <= 0 && ans == -1) ans = i + 1;    }    printf("%d\n", ans);    return 0;}

B
有毒,跳过

C
树型dp?直接把问题分解到子问题`进行递归,直接dfs

#include<stdio.h>#include<math.h>#include<string.h>#include<queue>#include<vector>#include<map>#include<algorithm>using namespace std;const int MAXN = 100000 + 5;vector<int> G[MAXN];int n;double ans = 0.0;double dfs(int v, int fa) {    int cnt = 0;    double res = 0.0;    for(int i = 0; i < G[v].size(); ++i) {        if(G[v][i] != fa) ++cnt;    }    for(int i = 0; i < G[v].size(); ++i) {        if(G[v][i] == fa) continue;        res += dfs(G[v][i], v) + 1;    }    return cnt ? res / cnt : 0.0;}int main(){    scanf("%d", &n);    for(int i = 1; i < n; ++i) {        int x, y;        scanf("%d%d", &x, &y);        G[x].push_back(y);        G[y].push_back(x);    }    ans = dfs(1, 1);    printf("%.15f\n", ans);    return 0;}

D
可以参考 http://www.cnblogs.com/quintessence/p/7354412.html
不过感觉他推倒公式有个小bug,等式最左边应该是

(i=0cntiC(cnt,i))1

#include<stdio.h>typedef long long LL;const int MOD  = (int)1e9 + 7;const int MAXN = (int)1e6 + 5;int num[MAXN], res[MAXN], MA;int modpow(int a, int n) {    int res = 1;    while(n) {        if(n & 1) res = 1LL * res * a % MOD;        a = 1LL * a * a % MOD;        n >>= 1;    }    return res;}void add(int x) {    int i = 2;    while(i * i < x) {        if(x % i == 0) {            ++num[i];            ++num[x / i];        }        ++i;    }    if(i * i == x) ++num[i];    ++num[x];}int main(){    int n;    scanf("%d", &n);    for(int i = 0; i < n; ++i) {        int x;        scanf("%d", &x);        add(x);        if(x > MA) MA = x;    }    int ans = 0;    for(int i = MA; i > 1; --i) {        if(num[i] == 0) continue;        res[i] = 1LL * num[i] * modpow(2, num[i] - 1) % MOD;        for(int j = 2; i * j <= MA; ++j) {            res[i] = (res[i] - res[i * j] + MOD) % MOD;        }        ans = (ans + 1LL * i * res[i]) % MOD;    }    printf("%d\n", ans);    return 0;}
原创粉丝点击