codeforces.com/contest/839/

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值