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

 

posted @ 2018-04-21 23:04  Aragaki  阅读(299)  评论(0编辑  收藏  举报