Codeforces Round #548 (Div. 2)

比赛链接
cf

A

最后一位判定

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#define P(x, y) 1ll * (x) * inv(y) % P
using namespace std;
typedef long long ll;
const int N = 65005;
const ll P = 1e9 + 7;
int n;
ll ans;
char str[N];
int main(){
    scanf("%d%s", &n, str + 1);
    for(int i = 1; i <= n; ++i){
        if(!((str[i] - '0') & 1)) ans += i;
    }
    printf("%lld", ans);
    return 0;
}

B

倒叙维护最大值

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#define P(x, y) 1ll * (x) * inv(y) % P
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
const ll P = 1e9 + 7;
int n, a[N], lim;
ll ans;
int main(){
    scanf("%d", &n);
    for(int i = 1, x; i <= n; ++i){
        scanf("%d", &a[i]);
    }
    lim = a[n]; ans += a[n];
    for(int i = n - 1; i >= 1; --i){
        lim = max(0, min(lim - 1, a[i]));
        ans += lim;
    }
    printf("%lld\n", ans);
    return 0;
}

C

所有方案 - 全是红边的方案
并查集维护

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e5 + 5;
const ll P = 1e9 + 7;
int n, m; ll ans;
int size[N], fa[N];
inline ll qpow(ll x, ll y){
    ll res = 1;
    while(y){
        if(y & 1) res = res * x % P;
        x = x * x % P; y >>= 1;
    }
    return res;
}
int find(int x){return x == fa[x] ? x : fa[x] = find(fa[x]);}
inline void merge(int x, int y){x = find(x), y = find(y), fa[x] = y, size[y] += size[x];}
int main(){
    scanf("%d%d", &n, &m);
    ans = qpow(n, m);
    for(int i = 1; i <= n; ++i) fa[i] = i, size[i] = 1;
    for(int i = 1, x, y, z; i < n; ++i){
        scanf("%d%d%d", &x, &y, &z);
        if(!z) merge(x, y);
    }
    for(int i = 1; i <= n; ++i){
        if(fa[i] == i){
            ans = (ans + P - qpow(size[i], m)) % P;
        }
    }
    printf("%lld\n", ans);
    return 0;
}

然后我就自闭了?

D

给定数m \(m \leq 1e5\)
进行如下流程:
1.rand一个[1, m]的数
2.把它接在序列a后面
3.如果a的gcd等于1 退出 否则进行第一步
求进行流程次数的期望

前面那个\(\frac{(m-m/x)}{m}\)是抽到的数不是自己的倍数的概率
\(\frac{(m-m/x)}{m}*dp[x] = 1 + \sum_{d|x}^{m} \frac{dp[d]*c[d]}{m}\)
当然容斥那里用莫比乌斯函数也ok啦【因为懒所以这么写了

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e5 + 5;
const ll P = 1e9 + 7;
inline ll qpow(ll x, ll y){
    ll res = 1;
    while(y){
        if(y & 1) res = res * x % P;
        x = x * x % P; y >>= 1;
    }
    return res;
}
int cnt[N], m, invm;
vector<int> d[N];
ll f[N];
inline void init(){
    for(int i = (m >> 1); i > 1; --i)
        for(int j = (i << 1); j <= m; j += i)
            d[j].push_back(i);
    invm = qpow(m, P - 2);
}
inline void cut(int x){
    for(int i : d[x]) cnt[i] -= cnt[x];
}
int main(){
    scanf("%d", &m), init();
    for(int i = 2; i <= m; ++i){
        for(int j : d[i]) cnt[j] = m / j;
        cnt[i] = m / i, cut(i);
        for(int j : d[i]){
            f[i] = (f[i] + f[j] * cnt[j] % P) % P;
            cut(j);
        }
        f[i] = ((f[i] + 1ll * m) % P * qpow(m - m / i, P - 2) % P) % P;
    }
    ll ans = 0;
    for(int i = 2; i <= m; ++i) ans = (ans + f[i]) % P;
    ans = (ans * invm % P + 1) % P;
    printf("%lld", ans);
    return 0;
}

E

有n个点 m个集合
每个点有一个权值pi 有一个集合编号ci
每次删掉一个点之后查询每个集合选一个权值的最大mex
所有数据小于等于5000

倒着加边 边加边匹配

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define mp(x, y) make_pair(x, y)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 5005;
int n, m;
struct Edge{int v, next;}edge[N];
int head[N], esize;
inline void addedge(int x, int y){
    edge[++esize] = (Edge){y, head[x]}, head[x] = esize;
}
int p[N], c[N], day[N], ans;
int d, bk[N], match[N];
bool vis[N], del[N];
bool dfs(int x){
    for(int i = head[x], vv; ~i; i = edge[i].next){
        vv = edge[i].v;
        if(!vis[vv]){
            vis[vv] = 1;
            if(!match[vv] || dfs(match[vv])){
                match[vv] = x; return 1;
            }
        }
    }
    return 0;
}
int main(){
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i) scanf("%d", &p[i]), ++p[i];
    for(int i = 1; i <= n; ++i) scanf("%d", &c[i]);
    scanf("%d", &d);
    for(int i = 1; i <= d; ++i) scanf("%d", &bk[i]), del[bk[i]] = 1;
    ans = 1;
    for(int i = 1; i <= n; ++i) if(!del[i]) addedge(p[i], c[i]);
    for(int i = d; i >= 1; --i){
        memset(vis, 0, sizeof(vis));
        while(dfs(ans)){++ans; memset(vis, 0, sizeof(vis));}
        day[i] = ans - 1;
        addedge(p[bk[i]], c[bk[i]]);
    }
    for(int i = 1; i <= d; ++i) printf("%d\n", day[i]);
    return 0;
}

F

太神仙了慢慢搞
一眼扫描线?

原文地址:https://www.cnblogs.com/hjmmm/p/10828662.html

时间: 2024-10-27 19:55:47

Codeforces Round #548 (Div. 2)的相关文章

Codeforces Round #548 (Div. 2) C dp or 排列组合

https://codeforces.com/contest/1139/problem/C 题意 一颗有n个点的树,需要挑选出k个点组成序列(可重复),按照序列的顺序遍历树,假如经过黑色的边,那么这个序列就是好的,问有多少个好的序列 题解 黑边不连,红边连,假如两个点不在同一并查集,那么一定经过黑边 定义\(dp[i][j][k]\)为选择前i个点,起始点为j,是否已经经过黑边(k)的方案数 \(dp[i-1][j][0]*(n-N[fin(j)])+dp[i-1][j][1]*n - > dp

Codeforces Round #548 (Div. 2) F splay(新坑) + 思维

https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\),\(pref_j\),一个人可以买一道菜的条件是 1. \(p_i \leq inc_j \leq s_i\) 2. \(|b_i - pref_j| \leq inc_j-p_i\) ,问每个人分别能买多少道菜 题解 转化一下公式 \(p_i \leq inc_j \leq s_i\) 下面两个满

C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree (a connected undirected graph without cycles) of nn vertices. Each of the n−1n−1 edges of the tree is col

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f