首页 > 其他分享 >Atcoder ABC282E Choose Two and Eat One

Atcoder ABC282E Choose Two and Eat One

时间:2023-02-02 08:44:26浏览次数:67  
标签:Atcoder tot return int ABC282E Two long fa Choose

https://atcoder.jp/contests/abc282/tasks/abc282_e


发现选出两个球去掉一个球其实很像一颗树去掉叶子节点,贡献即为叶子节点与父亲的边权

那这题就很明显了,预处理好每两个数产生的贡献,跑一个最大生成树即可。

时间复杂度 \(O(n\sum \log a_i + n^2\log n^2)\)。


# include <bits/stdc++.h>
using namespace std;
const int N = 500 + 10;
struct line {
    int u, v;
    long long w;
} a [N * N];
long long m;
long long qmi (long long a, long long b) {
    long long tot = 1;
    while (b) {
        if (b & 1) {
            tot *= a;
            tot %= m;
        }
        a *= a;
        a %= m;
        b >>= 1;
    }
    return tot;
}// 快速幂
long long p [N];
int fa [N];
int find (int x) {
    if (fa [x] == x) {
        return x;
    }
    return fa [x] = find (fa [x]);
}
int main () {
    int n;
    scanf ("%d %lld", & n, & m);
    for (int i = 1; i <= n; ++ i) {
        scanf ("%lld", & p [i]);
    }
    int cnt = 0;
    for (int i = 1; i <= n; ++ i) {
        for (int j = i + 1; j <= n; ++ j) {
            a [++ cnt] = {i, j, (qmi (p [i], p [j]) + qmi (p [j], p [i])) % m};
        }
    }// 处理好贡献
    sort (a + 1, a + cnt + 1, [] (line a, line b) {
        return a .w > b .w;
    });
    for (int i = 1; i <= n; ++ i) {
        fa [i] = i;
    }
    long long ans = 0;
    for (int i = 1, j = 0; j < n - 1; ++ i) {
        int u = find (a [i] .u), v = find (a [i] .v);
        if (u != v) {
            ans += a [i] .w;
            fa [u] = v;
            ++ j;
        }
    }// 最大生成树
    printf ("%lld", ans);
    return 0;
}

标签:Atcoder,tot,return,int,ABC282E,Two,long,fa,Choose
From: https://www.cnblogs.com/lctj-bot/p/17084718.html

相关文章

  • Atcoder ABC282H Min + Sum
    https://atcoder.jp/contests/abc282/tasks/abc282_h挂了好久发现二分写挂了……对于\(\min\{a_i\}\)这一部分,不难想到找到当前\(\min\{a_i\}\)的位置计算其左右两......
  • AtCoder Beginner Contest 287
    FComponents考虑树形\(DP\)。有\(f_{i,j,0/1}\)为以\(i\)为根的子树,一共有\(j\)个连通块,选/不选的方案数。\[pre_{x,0/1}\leftarrowf_{u,x,0/1}\]\[f_......
  • JSTL常用标签choose和foreach常用标签
    JSTL的常用标签choosechoose相当于java代码中的switch语句完成数字编号对应星期几案例1、域中存储数字2、使用choose标签取出数字 相当于switch声明......
  • AtCoder Beginner Contest 285
    C:abc285_brutmhyhiizp题目大意一串序列A,B,...,Z,AA,AB,...,ZZ,AAA,...给定一个字符串求在序列中的排名(保证存在)思路将每个A看作\(1\),B看作\(2\),....,Z......
  • (AtCoder Beginner Contest 287)(D 字符串前后缀合并匹配)(E Trie求最长公共前缀(LCP)
    D-MatchorNot(字符串前后缀合并匹配)题目大意:给定两个字符串S和T,对于每个x=0,1,2...|T|求S的子串(前x个字符和后|T|-x个字符在不改变顺序的情况下组成)是否与T相......
  • 【字典树】Atcoder Beginner Contest 287----E
    题目:E-Karuta(atcoder.jp)题解:这道题就是一个字典树求最长公共前缀的板子题。可以直接交板子。但我在翻题解的时候发现了另一种思路,就是将字符串按字典序排列后,每一个......
  • AtCoder Beginner Contest 287 解题报告
    AtCoderBeginnerContest287解题报告\(\text{ByDaiRuiChen007}\)ContestLinkA.Majority用map分别统计两种字符串出现的次数并与\(\left\lfloor\dfracn2\righ......
  • AtCoder Beginner Contest 287
    A-Majority(abc287a)题目大意给定\(n\)个人对某个提案的意见,问大多数意见是支持还是反对解题思路统计比较即可。神奇的代码#include<bits/stdc++.h>usingnam......
  • AtCoder Beginner Contest 287
    纯纯手速场C首先这张图必须是一棵树,必有\(M=N-1\)。接下来只需求出树的直径,判断其长度(边数)是否为\(N-1\)即可。https://atcoder.jp/contests/abc287/submissions/3......
  • Atcoder Beginner Contest 287
    赛时吃了三个法师,不过问题不大。赛时AB简单字符串处理。C中需要满足:\(m=n-1\)只有两个度数为\(1\)的点,剩下点的度数都为\(2\)。记得判连通!!D根据题目要求观......