• Codeforces 964 等比数列逆元处理 贪心删偶数度节点


    A

    B

    C

    注意sum要在mod范围内 且不能用/a*b来推

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    ll num[100005];
    const ll mod = 1e9 + 9;
    ll qpow(ll a, ll b)
    {
            ll ans = 1, base = a;
            while (b)
            {
                    if (b & 1)
                    {
                            ans = (ans * base) % mod;
                    }
                    base = (base * base) % mod;
                    b >>= 1;
            }
            return ans;
    }
    ll anser = 0;
    ll sum = 0;
    ll ok(ll x)
    {
            return (x % mod + mod) % mod;
    }
    int main()
    {
            ll n, a, b, k;
            cin >> n >> a >> b >> k;
            string str;
            cin >> str;
            ll now = 0;
            for (ll i = 0; i < k; i++)
            {
                    now = (qpow(a, n - i) * qpow(b, i)) % mod;
                    if (str[i] == '-')
                    {
                            sum = (sum - now + mod) % mod;
                    }
                    else
                    {
                            sum = (sum + now) % mod;
                    }
            }
            ll q = (qpow(b, k) * qpow(qpow(a, mod - 2), k)) % mod;
            if (q == 1)
            {
                    cout << (sum * (n + 1) / k) % mod << endl;
                    return 0;
            }
            ll maxn = max(a, b);
            ll minn = min(a, b);
            ll chu1 = qpow(qpow(a, n + 1 - k), mod - 2);
            ll chu2 = qpow(qpow(maxn, k) - qpow(minn, k), mod - 2);
            chu2 = ok(chu2);
            ll beichu = (qpow(maxn, n + 1) - qpow(minn, n + 1)) % mod;
            beichu = ok(beichu);
            anser = (((((sum * beichu) % mod) * chu1) % mod) * chu2) % mod;
            anser = ok(anser);
            cout << anser << endl;
    }
    View Code

    D

    先从叶子节点开始删起 因为如果删除了父亲节点的话 子节点如果是偶数就变成奇数可能会永远删不掉

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const ll mod = 998244353;
    vector<int> tree[200005];
    stack<int> q;
    int du[200005];
    int father[200005];
    int visit[200005];
    queue<int> anser;
    queue<int> que;
    int root;
    void dfs(int x, int pre)
    {
            father[x] = pre;
            int len = tree[x].size();
            for (int i = 0; i < len; i++)
            {
                    int to = tree[x][i];
                    if (to == pre)
                    {
                            continue;
                    }
                    dfs(to, x);
            }
    }
    void doit(int x)
    {
            anser.push(x);
            visit[x] = 1;
            int len = tree[x].size();
            for (int i = 0; i < len; i++)
            {
                    int to = tree[x][i];
                    du[to]--;
                    if (visit[to] || father[x] == to)
                    {
                            continue;
                    }
                    if (du[to] % 2 == 0)
                    {
                            doit(to);
                    }
            }
    }
    int main()
    {
            int n;
            cin >> n;
            for (int i = 1; i <= n; i++)
            {
                    int now;
                    cin >> now;
                    if (now == 0)
                    {
                            root = i;
                            continue;
                    }
                    tree[i].push_back(now);
                    tree[now].push_back(i);
                    du[i]++, du[now]++;
            }
            if ((n - 1) % 2)
            {
                    cout << "NO" << endl;
                    return 0;
            }
            dfs(root, 0);
            que.push(root);
            while (!que.empty())
            {
                    int x = que.front();
                    que.pop();
                    q.push(x);
                    int len = tree[x].size();
                    for (int i = 0; i < len; i++)
                    {
                            int to = tree[x][i];
                            if (to == father[x])
                            {
                                    continue;
                            }
                            que.push(to);
                    }
            }
            while (!q.empty())
            {
                    if (du[q.top()] % 2 == 0)
                    {
                            doit(q.top());
                    }
                    q.pop();
            }
            if (anser.size() != n)
            {
                    cout << "NO" << endl;
                    return 0;
            }
            cout << "YES" << endl;
            while (!anser.empty())
            {
                    cout << anser.front() << endl;
                    anser.pop();
            }
    }
    View Code
  • 相关阅读:
    ios启动画面
    不让精灵移除屏幕外 重写setPosition方法
    post和get请求方式的区别
    IOS开发之手势——UIGestureRecognizer 共存
    Xcode 中的GDB的命令
    [UIView beginAnimations:context:]与[UIView animateWithDuration:animations:]值得注意的一个区别
    应用图标 ICON
    cocos2d 1.01不能运行以前版本工程的问题
    SQL 中数据类型的转换 转
    SQL Server中sql语句执行时间
  • 原文地址:https://www.cnblogs.com/Aragaki/p/8904191.html
Copyright © 2020-2023  润新知