首页 > 其他分享 >Codeforces Round 891 (Div. 3)

Codeforces Round 891 (Div. 3)

时间:2023-08-09 10:47:23浏览次数:44  
标签:891 return int res rhs Codeforces constexpr Div MInt

A. Array Coloring

#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n;
    cin >> n;
    int sum = 0;
    for( int i = 1 , x ; i <= n ; i ++ )
        cin >> x , sum += x;
    if( sum % 2 == 0 ) cout << "YES\n";
    else cout << "NO\n";
    return ;
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--)
        solve();
    return 0;
}

B. Maximum Rounding

模拟一下进位就好了

#include <bits/stdc++.h>

using namespace std;

void solve() {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    vector<int> a;
    for( auto i : s )
        a.push_back( i - '0' );
    for( int i = 1 ; i <= 5 ; i ++ ) a.push_back(0);
    int p = -1;
    for( int i = 0 ; i+1 < a.size() ; i ++ ){
        a[i+1] += a[i] / 10 , a[i] %= 10;
        if( a[i] > 4 ) a[i+1] ++ , p = i;
    }
    for( int i = 0 ; i <= p ; i ++ ) a[i] = 0;
    while( a.back() == 0 ) a.pop_back();
    reverse(a.begin(), a.end());
    for( auto i : a )
        cout << i ;
    cout << "\n";
    return ;

}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--)
        solve();
    return 0;
}

C. Assembly via Minimums

其实这里面数字的顺序是没有影响的。所以规定原始数组为递增的即可。然后比第一个数组大的数字有\(n-1\)个比,第二个数字大的有\(n-2\)个以此类推,所以比第\(i\)个数字大的有\(n-i\)个。因为读入的数字是合法的,所以我们把读入数组排序,然后每次跳\(n-i\)即可

#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n , m;
    cin >> n , m = n * ( n - 1 ) / 2 ;
    vector<int> a(m);
    for( auto & i : a ) cin >> i;
    sort( a.begin(), a.end() );
    for( int i = 1 , j = 0 ; i < n ; i ++ ){
        cout << a[j] << " ";
        j += n - i;
    }
    cout << a.back() << "\n";
    return ;

}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--)
        solve();
    return 0;
}

D. Strong Vertices

一个非常经典的套路

\[a_u-a_v\ge b_u-b_v\Rightarrow a_u-b_u\ge a_v-b_v \]

所以我们把差值排序,看有多少个和最大值相同即可。

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    int n, m;
    cin >> n;
    vector<pair<int,int>> a(n);
    for( int i = 0 ; i < n ; i ++ )
        cin >> a[i].first , a[i].second = i+1;
    for( int i = 0 , x ; i < n ; i ++ )
        cin >> x , a[i].first -= x;
    sort( a.begin(), a.end());
    vector<int> res;
    for( int i = n - 1 ; i >= 0 ; i -- ){
        if( a[i].first == a.back().first ) res.push_back(a[i].second);
        else break;
    }
    sort( res.begin(), res.end() );
    cout << res.size() << "\n";
    for( auto i : res )
        cout << i << " ";
    cout << "\n";
    return;

}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--)
        solve();
    return 0;
}

E. Power of Points

这题要离线做,把区间读进来后,按照\(x_i\)排序,然后就可用前缀后缀和来做这题,具体的做法其实就是提共因式。

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    int n, m;
    cin >> n;
    vector<pair<int, int>> a(n + 1);

    int x = 0, y = 0;

    for (int i = 1; i <= n; i++)
        cin >> a[i].first, a[i].second = i, y += a[i].first;
    vector<int> res(n + 1);
    sort(a.begin(), a.end());
    for (int i = 1, cnt; i <= n; i++) {
        res[a[i].second] += y - (n - i + 1) * (a[i].first - 1);
        res[a[i].second] += (a[i].first + 1) * (i - 1) - x;
        y -= a[i].first, x += a[i].first;
    }
    for( int i = 1 ; i <= n ; i ++ )
        cout << res[i] << " ";
    cout << "\n";
    return;

}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--)
        solve();
    return 0;
}

F. Sum and Product

这题还是很好想的,设\(a=a_i,b=a_j\)

\[a+b=x,ab=y\\ a = x-b\\ (x-b)b=y\Rightarrow b^2-xb+y=0 \]

解出一元二次方程,然后计算出\(a,b\)的值,就可以用组合数来统计答案。

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    int n, m;
    cin >> n;
    map<int, int> cnt;
    for (int i = 1, x; i <= n; i++)
        cin >> x, cnt[x]++;
    int q;
    cin >> q;
    for (int x, y, t, a, b; q; q--) {
        cin >> x >> y;
        t = x * x - 4 * y;
        if (t < 0) {
            cout << "0 ";
            continue;
        }
        a = (x + sqrt(t)) / 2, b = x - a;
        if (a * b != y) {
            cout << "0 ";
            continue;
        }
        if (a == b) cout << cnt[a] * (cnt[a] - 1ll) / 2ll << " ";
        else cout << cnt[a] * cnt[b] << " ";
    }
    cout << "\n";
    return;

}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--)
        solve();
    return 0;
}

G. Counting Graphs

这道题我赛时没有出,还是比较惭愧的。

考虑Kruskal的过程,如果对于当前加了一条边\(u,v,w\),那么在\(u\)所在的联通快中任意一个点和\(v\)所在的联通块中任意一个点加一条权值大于\(w\)的边都是无意义的。

所以我们可以用并查集模拟 Kruskal的过程,对于当前边\((u,v,w)\)对答案产生的贡献是\((S-w+1)^{size_u\times size_v - 1}\)

把贡献累积就是答案。

#include <bits/stdc++.h>

using namespace std;

#define int long long

class dsu {
private:
    vector<int> fa;
public:
    dsu(int n = 1) {
        fa = vector<int>(n + 1, -1), fa[0] = 0;
    }

    int getfa(int x) {
        if (fa[x] < 0) return x;
        return fa[x] = getfa(fa[x]);
    }

    void merge(int x, int y) {
        x = getfa(x), y = getfa(y);
        if (x == y) return;
        if (fa[x] > fa[y]) swap(x, y);
        fa[x] += fa[y], fa[y] = x;
    }

    bool same(int x, int y) {
        x = getfa(x), y = getfa(y);
        return (x == y);
    }

    int size(int x) {
        x = getfa(x);
        return -fa[x];
    }
};

template<class T>
constexpr T power(T a, int b) {
    T res = 1;
    for (; b; b /= 2, a *= a)
        if (b % 2) res *= a;
    return res;
}

template<int P>
struct MInt {
    int x;
    static int Mod;

    constexpr MInt() : x(0) {};

    constexpr MInt(int x) : x(norm(x % getMod())) {};

    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }

    constexpr static int getMod() {
        if (P > 0) return P;
        else return Mod;
    }

    constexpr int norm(int x) const {
        if (x < 0) x += getMod();
        if (x >= getMod()) x -= getMod();
        return x;
    }

    constexpr int val() const {
        return x;
    }

    explicit constexpr operator int() const {
        // 隐式类型转换把 MInt转换成 int
        return x;
    }

    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }

    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }

    constexpr MInt &operator*=(MInt rhs) &{
        x = x * rhs.x % getMod();
        return *this;
    }

    constexpr MInt &operator+=(MInt rhs) &{
        x = norm(x + rhs.x);
        return *this;
    }

    constexpr MInt &operator-=(MInt rhs) &{
        x = norm(x - rhs.x);
        return *this;
    }

    constexpr MInt &operator/=(MInt rhs) &{
        return *this *= rhs.inv();
    }

    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }

    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }

    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }

    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }

    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }

    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        int v;
        is >> v;
        a = MInt(v);
        return is;
    }

    friend constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }

    friend constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
};

using Z = MInt<998244353>;


void solve() {
    int n , s ;
    cin >> n >> s;
    vector<tuple<int, int, int>> e(n - 1);
    for (auto &[w, u, v]: e)
        cin >> u >> v >> w;
    sort(e.begin(), e.end());
    dsu d(n);
    Z res(1);
    for( auto [w,u,v] : e ){
        int cnt = d.size(u) * d.size(v) - 1;
        res *= power( Z( s - w + 1 ) , cnt );
        d.merge( u , v );
    }
    cout << res << "\n";
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int t;
    cin >> t;
    for (; t; t--)
        solve();

    return 0;
}

标签:891,return,int,res,rhs,Codeforces,constexpr,Div,MInt
From: https://www.cnblogs.com/PHarr/p/17616221.html

相关文章

  • Codeforces Round 891 (Div. 3) 题解
    A.ArrayColoring因为:偶数+偶数=偶数奇数+奇数=偶数奇数+偶数=奇数所以设\(s1\)为奇数之和,\(s2\)为偶数之和\(s2\)必定是偶数如果奇数的个数为偶数,则\(s1\)为偶数;否则是奇数而在\(s1\)为奇数时,即使拿一个奇数加到\(s2\)里,那么也是不合法的所以直接判断奇数的......
  • Codeforces Round 891 (Div. 3)
    CodeforcesRound881(Div.3)A.ArrayColoring题目大意link思路简单判断数组和的奇偶性即可(小学数学)#include<bits/stdc++.h>usingnamespacestd;intmain(){intt;cin>>t;while(t--){intn;cin>>n;intsum=......
  • UESTC 2023 Summer Training #23 for div2/2022-2023 ACM-ICPC Latin American Region
    Preface今天这场签到巨多,和昨天那场形成了鲜明的对比但可惜后盘的时候我划了太久的水,最后接了B题然后没调出来成为战俘最气的是赛后发现原来是没注意输出格式,本来可以说一遍过的题结果没写过,属实可惜,就当长教训了以后一定要尤其注意输入输出格式A.AskingforMoneyORZ徐......
  • Codeforces Round 891 (Div. 3)
    CodeforcesRound891(Div.3)A-ArrayColoring思路:需要两部分的奇偶相同,判断奇数的个数是否为偶数即可#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong//#defineint__int128#definedoublelongdoubletypedefpair<int,int>PII;typedefpair......
  • CodeForces CF1846G 题解
    CodeForcesCF1846G题解CodeForces题目链接洛谷题目链接标准答案是状压之后,转化成Dijkstra算法跑最短路。我这里提供一个不一样的思路。题意简述主人公得了病,有部分类型的症状。所有类型症状共有\(n\)种,用长为\(n\)的01串表示是否有那种症状。共有\(m\)种药,吃......
  • Codeforces 890-891的一些题目的反思
    和atcoder一起出交互题是吧。D题回复逆序对个数,对于[L,R-1]和[L,R],如果R是最大值,那么对逆序对个数无影响。这样来确认某个数是不是最大的,然后递归扩展到整个区间这里看到逆序对,要想到归并排序、分治、递归、区间合并。。。。。查看代码//Problem:D.MoreWrong//Contest......
  • B. Longest Divisors Interval
    link需要思考一下如果这个题能做,那么肯定有一种比较可行的做法。如果\([l,r]\)是可行的,那么就意味着\([1,r-l+1]\)是可行的这是显然的,显然后者的每一个数在前者中必然有对应的倍数,所以等效。这样从1开始找就行了。#include<cstdio>#include<iostream>#include<cstring>#i......
  • vue问题:不存在div或者多个div
    <el-radiov-model="radio"label="1">备选项</el-radio><el-radiov-model="radio"label="2">备选项</el-radio>报错:Errorscompilingtemplate:Componenttemplateshouldcontainexactlyonerootelement.......
  • codeforces 891 (div3)857E - Power of Points
    E.点的力量每个测试限时2秒每个测试限制内存为256兆字节输入以标准格式输入输出以标准格式输出给定n个具有整数坐标x1,…xn的点,这些点位于数线上。对于某个整数s,我们构建段[s,x1],[s,x2],…,[s,xn]。注意,如果xi<s,则段将类似于[xi,s]。段[a,b]覆盖了所有整数点a,a+1,a+2,…,b。......
  • 【CF】#844 div1 T1~T4复健
    高考结束,我的人生即将迈入新的阶段。记得哪位退役学长说的话,尽管努力不够,天赋不足,但走进大学校园,我仍将拾起键盘。所以打了场cf比赛,没想到前几道题都不涉及算法板子,但断断续续做了好几天也才做了四个题。T5终于忍不住找了题解,一看是二分图可惜早已忘光,做不出来。前四道题不涉及......