首页 > 其他分享 >AtCoder Beginner Contest 280 A-F

AtCoder Beginner Contest 280 A-F

时间:2022-12-04 21:01:32浏览次数:62  
标签:AtCoder cnt cout Beginner int long 280 find dis

AtCoder Beginner Contest 280 A-F

https://atcoder.jp/contests/abc280
个人认为D >> E,F
被D搞心态了,导致EF都没看()

A - Pawn on a Grid

统计#的个数

#include <bits/stdc++.h>

using namespace std;

int main () {
    int n, m, cnt = 0;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            char ch;
            cin >> ch;
            if (ch == '#')  cnt ++;
        }
    }
    cout << cnt;
}

B - Inverse Prefix Sum

根据前缀和还原原数组

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

using namespace std;
const int N = 15;
int a[N], n;

signed main () {
    cin >> n;
    for (int i = 1; i <= n; i++)    cin >> a[i], cout << a[i] - a[i-1] << ' ';
}

C - Extra Character

s中插入一个字符变成t,求插入位置
注意特判末尾

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

using namespace std;

signed main () {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < s.size (); i++) {
        if (s[i] != t[i]) {
            cout << i + 1;
            return 0;
        }
    }
    cout << s.size () + 1;
}

D - Factorial and Multiple

先对k进行唯一分解,预处理出p和cnt数组,用来记录素因数及其个数
对于k, 大于\(\sqrt k\) 的因子只有一个,所以1e12的范围只需考虑到1e6即可。
因此如果k的最大素因子大于1e6,就直接取这个因子。
否则可以二分,范围从1到k,check当前mid是否能涵盖所有的因子。
(特别注意下tmp那里,我有点脑抽)

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

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

void test (int tt, int *p, int *cnt) {
    for (int i = 1; i <= tt; i++)   cout << p[i] << ' ' << cnt[i] << endl;
    cout << endl;
}

void pre () {
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            p[++tt] = i;
            while (n % i == 0) {
                n /= i;
                cnt[tt] ++;
            }
        }
    }
    if (n > 1) {
        p[++tt] = n;
        cnt[tt] ++;
    }
}

bool check (int x) {
    //cout << x << ": ";
    for (int i = 1; i <= tt; i++) {
        int tot = 0, pr = p[i], tmp = x;
        while (tmp) {
            tot += (tmp / pr);
            tmp /= pr;
            //cout << tmp << ' ';
        }
        //cout << "tot: " << tot << ' ';
        if (tot < cnt[i])   return false;
    }
    return true;
}

signed main () {
    cin >> n;
    int m = n;
    pre ();
    //test (tt, p, cnt);

    if (p[tt] > 1e6) {
        cout << n;
        return 0;
    }

    int l = 1, r = m;
    while (l < r) {
        int mid = l + r >> 1;
        if (check (mid))    r = mid;
        else    l = mid + 1;
        //cout << "end: " << l << ' ' << r << endl;
    }
    cout << r << endl;
}

//预处理素数表
//二分n
//chceck x!是否能覆盖所有分解

E - Critical Hit

题意:现在是0,有 \(\frac{p}{100}\) 的概率打出2的攻击,\(1-\frac{p}{100}\) 的概率打出1的攻击。求打出n的攻击的期望值,模998244353
分析:就是裸的期望dp,式子:\(f_i = (f_{i-2} * \frac{p}{100} + f_{i-1} * (1-\frac{p}{100})) + 1\),记得取模,分数求逆元。

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

using namespace std;
const int N = 2e5 + 5, mod = 998244353;
int n, p, f[N];

int qmi(int a,int k, int p) {
	int ans = 1;
	while (k) {
		if (k & 1)//末位1取出
			ans = ans * a % p;
		k >>= 1;//次末位
		a = a * a % p;
	}
	return ans;
}

signed main () {
    cin >> n >> p;
    f[1] = 1;
    int p1 = p * qmi (100, mod - 2, mod) % mod, p2 = (1 - p1 + mod) % mod;
    //cout << p1 << ' ' << p2 << endl;
    for (int i = 2; i <= n; i++) {
        f[i] = (f[i-1] * p2 % mod + f[i-2] * p1 % mod + 1) % mod;
    }
    cout << f[n];
}

F - Pay or Receive

题意:给定一个n个点的有向图,输入m条边,边 \({a,b,c}\) 对应一条 \(a\) 到 \(b\) 的权为 \(c\) 的边以及一条 \(b\) 到 \(a\) 的权为 \(-c\) 的边。然后q次询问,问从a点走到b点的最大距离。若不可达输出nan,若距离无穷输出inf,其余情况输出最大值。
分析:把图建起来之后,先判可达性。用并查集判两点是否在一个集合中。对于inf的情况,模拟样例可知,若两点之间可以经过正环,则可以通过在上面一直绕圈圈的方式达到无穷。所以对于每点出发,跑bfs,若发现到达同一点距离却不同的情况就是出现了正环。若无正环且可达,输出 \(dis_b-dis_a\)。

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

using namespace std;
const int N = 1e5 + 5, M = N * 2, inf = -1e18;
int n, m, q;
int h[N], e[M], ne[M], w[M], idx;
int fa[N], cnt[N], dis[N];
bool vis[N], isINF[N];

void add (int a, int b, int c) {
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}

int find (int x) {
    if (x != fa[x]) fa[x] = find (fa[x]);
    return fa[x];
}

void Merge (int a, int b) {
    a = find (a), b = find (b);
    if (a != b)     fa[a] = b;
}

void bfs (int st) {
    queue <int> q;
    q.push (st);
    vis[st] = true;
    dis[st] = 0;

    while (!q.empty ()) {
        auto t = q.front ();
        q.pop ();

        for (int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            if (dis[j] == inf) {
                dis[j] = dis[t] + w[i];
                vis[j] = true;
                q.push (j);
            }
            else if (dis[j] != dis[t] + w[i]) {
                isINF[find (st)] = true;
                return ;
            }
        }
    }
}

signed main () {
    memset (h, -1, sizeof h);
    cin >> n >> m >> q;
    for (int i = 1; i <= n; i++)    fa[i] = i, dis[i] = inf;
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        add (a, b, c), add (b, a, -c);
        if (find (a) != find (b))   Merge (a, b);
    }
    for (int i = 1; i <= n; i++) {
        int j = find (i);
        if (vis[j])     continue;
        vis[j] = true;
        bfs (j);
    }
    while (q --) {
        int a, b;
        cin >> a >> b;
        if (find (a) != find (b)) {
            cout << "nan\n";
            continue;
        }
        if (isINF[find (a)])    cout << "inf\n";
        else    cout << dis[b] - dis[a] << endl;
    }
}

//找正环

G - Do Use Hexagon Grid 2

似乎是一个很复杂的分类讨论,会看看的()

Ex - Substring Sort

蹲蹲大佬的题姐。

标签:AtCoder,cnt,cout,Beginner,int,long,280,find,dis
From: https://www.cnblogs.com/CTing/p/16950621.html

相关文章

  • AtCoder Beginner Contest 280
    D-FactorialandMultiple对\(k\)进行质因数分解。如果\(k\)最大的质因子\(p\)满足\(p*p>k\),那么答案就是\(p\)。因为一定要包含一次,也只需要包含一次。......
  • AtCoder Beginner Contest 280
    D-FactorialandMultiple数论  首先上这道题需要的数论知识:n!的素因子分解中,n!=p1^a1*p2^a2*p3^a3*.....*pk^ak中对于素因数pi,其对于的ai=n/pi+n/p......
  • abc--280--F
    F-PayorReceive题目大意给你一个图,查询两个点的最长路边权是正着走是正数,反着走为负数无法到达为nan,无穷大为inf注意:会有重边和自环思路用并查集划分连通块,用df......
  • abc--280--E
    题目大意一个人有n条命,你有p%的概率一次打它两条命,有(100-p)%的概率一次打他一条命求你打死它需要的次数的期望值思路其实也就和走台阶的那个题目是一样的,用dp写就行了......
  • abc--280--D
    D-FactorialandMultiple题目大意找到一个最小的n,使得n的阶乘能整除k思路将它化为一堆的质数,然后满足这些质数至少需要多大的数最后这个找最大的数的时候,直接暴力......
  • AtCoder Beginner Contest 280 题解
    A-PawnonaGrid看样例猜题意(要求的是#的数量,直接判断。//If,oneday,Ifinallymanagetomakemydreamsareality...//Iwonder,willyoustillbethere......
  • AtCoder Beginner Contest 280 D-E
    D-FactorialandMultiple前置知识\(n!\)中包含素因子\(p\)的个数为\[cnt=\sum\limits_{k\geq1}^{p^k\leqn}\lfloor\frac{n}{p^k}\rfloor\]例如:\(n!\)......
  • ORA-28040: 没有匹配的验证协议
    问题:ORA-28040:没有匹配的验证协议原因:Oracle数据库安装的是12.2版本,OracleClient安装的版本是11(ODTwithODAC1120320_32bit)。解决:打开 sqlnet.ora 文件,增加以下两行......
  • mysql学习系列:总结数据库连接不上的数种情况,问题编号:ERROR 1045 (28000)
    (文章目录)场景今天重启CDH的时候,发现重启报错,查看日志才发现是mysql数据库连接不上。在尝试解决的过程中,踩到一些坑。所以总结一下,并分享给大家看看,减少大家伙继续踩坑的......
  • AtCoder Beginner Contest 247 题解
    AtCoderBeginnerContest247Solution目录AtCoderBeginnerContest247Solution更好的阅读体验戳此进入题面链接题面Luogu链接A-MoveRight题面SolutionCodeB-U......