牛客周赛round-42 D-F

D-小红的树上删边

思路:

  • 树的dfs, 求子树大小
  • 如果此结点的子树结点个数为偶数,则代表可以分割,否则。代表不能分割。

以下是代码部分,代码参考来源——bilibili牛客官方题解

#include<bits/stdc++.h>
using namespace std;

using ll = long long;
int mod = 1e9 + 7;
const int N = 1e5 + 10;
vector<int> tree[N];
int sz[N], ans;

void dfs(int p, int father)
{
    sz[p] = 1;
    for(auto x : tree[p])
    {
        if(x == father) continue;
        dfs(x, p);
        sz[p] += sz[x];
    }
    if(p != 1 && sz[p] % 2 == 0)
        ans ++;
}

void solve()
{
    int n;
    cin >> n;
    for(int i = 1; i < n; i ++)
    {
        int u, v;
        cin >> u >> v;
        tree[u].push_back(v);
        tree[v].push_back(u);
    }
    dfs(1, -1);
    if(n & 1) cout << "-1\n";
    else cout << ans << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    solve();

    return 0;
}

E-小红的子序列求和

思路:

  • 求长度为k的数的和

  • 所以可以发现用组合数

  • 设字符串总长度为 n ,需要的子序列长度为k,设此时选定的字符为第i个字符(下标从1开始)。那么计算枚举它的每种方案,对总和的权值就可。

  • 难点在于组合数的计算

  • 下面有两种方法求组合数

    • 乘法逆元求组合数
    • 递推求组合数

解释无力,看代码。

  • 第一种求组合数的方法
    以下是代码部分
#include<bits/stdc++.h>
using namespace std;

using ll = long long;
int mod = 1e9 + 7;
const int N = 1e5 + 10;
ll a[N];
//存储阶乘
ll c[1010];
//存储快速幂
ll fc[1010];
string s;
//快速幂
ll quick(ll base, ll pow)
{
    ll res = 1;
    while(pow > 0)
    {
        if(pow & 1)
            res = res * base % mod;
        pow >>= 1;
        base = base * base % mod;
    }
    return res;
}
//乘法逆元
ll fac(ll m, ll n)
{
    if(m > n) return 0;
    return c[n] * fc[m] % mod * fc[n - m] % mod;
}

void solve()
{
    int n, k;
    cin >> n >> k >> s;
    s = ' ' + s;
    //递推求阶乘
    c[0] = 1;
    for(int i = 1; i <= n; i ++)
        c[i] = c[i - 1] * i % mod;
    //利用快速幂求乘法逆元
    for(int i = 0; i <= n; i ++)
        fc[i] = quick(c[i], mod - 2);
    ll sum = 0;
    for(int i = 1; i < s.size(); i ++)
    {
    //枚举s[i]的字符,分别为10进制中的第 (个位, 十位,百位……到第k位)所贡献的值
    //并求和
        for(int j = 0; j < k; j ++)
        {
            sum += (s[i] - '0') * quick(10, j) % mod * fac(k - j - 1, i - 1) % mod * fac(j, n - i) % mod;
            sum %= mod;
        }
    }
    //输出
    cout << sum << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    solve();

    return 0;
}
  • 第二种方法求组合数

以下是代码部分,代码参考来源——bilibili牛客官方题解

#include<bits/stdc++.h>
using namespace std;

using ll = long long;
int mod = 1e9 + 7;
ll c[1010][1010];
ll pow_10[1010];
string s;

void solve()
{
    //利用递推公式求组合数
    for(int i = 0; i <= 1000; i ++)
        for(int j = 0; j <= i; j ++) {
            if (j == 0 || i == j) c[i][j] = 1;
            else c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
        }
    pow_10[0] = 1;
    //预处理10的i次幂
    for(int i = 1; i <= 1000; i ++) pow_10[i] = pow_10[i - 1] * 10 % mod;
    int n, k;
    ll ans = 0;
    cin >> n >> k >> s;
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < k; j ++)
        {
            //     在10进制的第几位上     自身的大小      C(n, m)          后面共有n-i-1个数,选k-j-1个
            //                                        前面共有i个数,选j个
            ans += pow_10[k - j - 1] * (s[i] - '0') * c[i][j] % mod * c[n - i - 1][k - j - 1] % mod;
            ans %= mod;
        }
    cout << ans << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    solve();

    return 0;
}

F - 小红的扔骰子

思路:

  • 骰子总共3种情况。
  • dp[x][y] = {}代表dp[2的个数][3的个数] = {期望数}。
  • 那么可写出状态转移式
  • dp[x][y] = (dp[x - 1][y] + dp[x][y - 1] + dp[x][y]) * 1 / 3 + 1
  • 移项可得:dp[x][y] = (dp[x - 1][y] + dp[x][y - 1]) / 2 + 3 / 2
    • 解析:dp[x][y]由上一次转移而来,骰子可以骰三种情况,所以共由这三种情况转移而来,并且等概率,且概率均为1/3
  • 注意:除法需要用乘法逆元,不然会失去精度。

以下是代码部分,代码参考来源——bilibili牛客官方题解

#include<bits/stdc++.h>
using namespace std;

using ll = long long;
int mod = 1e9 + 7;

ll dp[80][80];
ll quick(ll base, ll pow)
{
    ll res = 1;
    while(pow)
    {
        if(pow & 1) res = base * res % mod;
        pow >>= 1;
        base = base * base % mod;
    }
    return res;
}

// x个2    y个3
ll f(ll x, ll y)
{
    if(x == 0) return 3 * y;
    if(y == 0) return 3 * x;
    if(dp[x][y]) return dp[x][y];
    ll inv_2 = quick(2, mod - 2);
    dp[x][y] = (f(x - 1, y) + f(x, y - 1)) * inv_2 % mod + 3 * inv_2 % mod;
    dp[x][y] %= mod;
    return dp[x][y];
}

void solve()
{
    ll x;
    cin >> x;
    int n_2 = 0, n_3 = 0;
    //统计共有几个2, 几个3。
    while(x % 2 == 0) x >>= 1, n_2 ++;
    while(x % 3 == 0) x /= 3, n_3 ++;
    //如果最后不为1,则无解,输出-1
    if(x != 1)
    {
        cout << -1 << '\n';
        return ;
    }
    cout << f(n_2, n_3) << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    solve();

    return 0;
}
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值