牛客小白月赛55 A-F

牛客小白月赛55 A-F

https://ac.nowcoder.com/acm/contest/38630

好像有点懒得写了www看看代码吧

A - 至至子的等差中项

#include <bits/stdc++.h>

using namespace std;

int main () {
    int a, b;
    cin >> a >> b;
    cout << 2*b-a;
}

B - 至至子的按位与

#include <bits/stdc++.h>
#define int long long

using namespace std;
int a, b;

void solve () {
    cin >> a >> b;
    int maxn = (1ll << 63) - 1ll;
    cout << maxn - (a ^ b) << endl;
}

signed main () {
    solve ();
}

C - 至至子的斐波那契

#include <bits/stdc++.h>
#define int long long

using namespace std;
vector <int> v;

void pre () {
    
    v.push_back (0), v.push_back (1), v.push_back (1);
    int x = 0;
    for (int i = 3; x <= 1e18; i++) {
        x = v[i-1] + v[i-2];
        v.push_back (x);
    }

    //for (auto i : v)    cout << i << ", ";
}

void solve () {
    int n, i;
    cin >> n;
    for (i = 1; ; i++) {
        if (v[i] >= n)  break;
    }
    if (v[i] - n >= n - v[i-1])  cout << i-1;
    else cout << i;
    cout << endl;
}

signed main () {
    pre ();
    int t;
    cin >> t;
    while (t --) {
        solve ();
    }
}

D - 至至子的鸿门宴

#include <bits/stdc++.h>
#define int long long

using namespace std;
const int N = 1e6 + 5;
int n, a[N];

signed main () {
    cin >> n;
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        sum += a[i] - i;
    }
    // b[1] = a[1];

    // for (int i = 1; i <= n; i++)    b[i] --, sum += b[i];
    //cout << sum << endl;
    if (sum % 2 == 0)   puts ("SSZ");
    else    puts ("ZZZ");

}

E - 至至子的长链剖分

#include <bits/stdc++.h>
#define int long long

using namespace std;
typedef pair<int, int> pii;
const int N = 2e5 + 5;
int n;

void solve () {
    cin >> n;
    vector <int> cnt[n+1]; //pre cnt
    int maxn = 0, x;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        cnt[x].push_back (i);
        maxn = max (maxn, x);
    }

    // for (int i = 0; i <= n; i++) {
    //     if (cnt[i].size() == 0) continue;
    //     cout << "i= " << i << ", ";
    //     for (auto j : cnt[i])
    //         cout << j << ' ';
    //     cout << endl;
    // }

    if (n == 1) {
        if (x)    cout << -1 << endl;
        else    cout << 1 << endl;
        return ;
    }

    if (cnt[maxn].size() > 1 || cnt[0].size() == 0)  {
        cout << -1 << endl;
        return ;
    }

    vector <pii> ans;
    for (int i = 1; i <= maxn; i++) {
        if (cnt[i].size() == 0 || cnt[i].size() > cnt[i-1].size()) {
            cout << "-1\n";
            return ;
        }
        int cnt1 = cnt[i-1].size(), cnt2 = cnt[i].size();
        for (int j = 0; j < cnt2; j++)  ans.push_back ({cnt[i-1][j], cnt[i][j]});
        for (int j = cnt2; j < cnt1; j++)   ans.push_back ({cnt[i-1][j], cnt[i][0]});
    }

    cout << cnt[maxn][0] << endl;
    for (auto i : ans)  cout << i.first << ' ' << i.second << endl;

}

signed main () {
    int t;
    cin >> t;
    while (t --) {
        solve ();
    }
}

//h为几就在第几+1层(从下往上)
//相邻数字连一条边
//并且上层的个数一定要小于等于下层的

//之前是把所有的都连到一点上所以就错了
//上一层与下一层尽量相连,然后多的在统一连到一点

F - 至至子的公司排队

树形 dp + 可重集组合
套用了jls 的取模类板子

牛客官方题解:

来源

#include <bits/stdc++.h>

using namespace std;

constexpr int P = 1000000007;
using i64 = long long;
// assume -P <= x < 2P
int norm(int x) {
    if (x < 0) {
        x += P;
    }
    if (x >= P) {
        x -= P;
    }
    return x;
}
template<class T>
T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}
struct Z {
    int x;
    Z(int x = 0) : x(norm(x)) {}
    Z(i64 x) : x(norm(x % P)) {}
    int val() const {
        return x;
    }
    Z operator-() const {
        return Z(norm(P - x));
    }
    Z inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    Z &operator*=(const Z &rhs) {
        x = i64(x) * rhs.x % P;
        return *this;
    }
    Z &operator+=(const Z &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    Z &operator-=(const Z &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    Z &operator/=(const Z &rhs) {
        return *this *= rhs.inv();
    }
    friend Z operator*(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res *= rhs;
        return res;
    }
    friend Z operator+(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res += rhs;
        return res;
    }
    friend Z operator-(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res -= rhs;
        return res;
    }
    friend Z operator/(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res /= rhs;
        return res;
    }
    friend std::istream &operator>>(std::istream &is, Z &a) {
        i64 v;
        is >> v;
        a = Z(v);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const Z &a) {
        return os << a.val();
    }
};

int main () {
    int n, tot = 0;
    cin >> n;
    Z ans = 1;
    
    for (int i = 0; i < n; i++) {
        int m;  cin >> m;
        for (int j = 0; j < m; j++)     ans *= ++ tot;

        vector <vector<int>> e(m);
        for (int j = 1; j < m; j++) {
            int x;  cin >> x;
            x --;
            e[x].push_back (j);
        }

        vector <int> sz(m, 1);
        function<void(int)> dfs = [&] (int x) {
            for (auto y : e[x]) {
                dfs (y);
                sz[x] += sz[y];
            }
            ans /= sz[x];
        };
        dfs (0);
    }
    cout << ans << endl;
}

//jls的取模类板子
posted @ 2022-08-20 00:37  Sakana~  阅读(45)  评论(0编辑  收藏  举报