首页 > 其他分享 >AtCoder Beginner Contest 300

AtCoder Beginner Contest 300

时间:2023-05-03 21:44:06浏览次数:35  
标签:prime AtCoder ch frac Beginner 300 ++ int &&

A - N-choice question

#include<bits/stdc++.h>

using namespace std;

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

const int N = 1e5+5;
bool vis[N];
vector<int> e[N] ;
int d[N];

int main() {
	int n = read() , a = read() , b = read();
    for( int x , i = 1 ; i <= n ; i ++ ){
        x = read();
        if( x == a + b ) cout << i , exit(0);
    }
    
    return 0;
}

B - Same Map in the RPG World

看起来似乎很复杂,其实可以直接枚举横着移动多少次,竖着移动多少次就行了。

#include<bits/stdc++.h>

using namespace std;


int main() {
	ios::sync_with_stdio(false) , cin.tie(nullptr) , cout.tie(nullptr);
    int n , m;
    cin >> n >> m;
    vector<string> a(n) , b(n);
    for( auto & i : a ) cin >> i;
    for( auto & i : b ) cin >> i;
    for( int r = 0 ; r < n ; r ++ )
        for( int c = 0 ; c < m ; c ++ ){
            int f = 1;
            for( int i = 0 ; f && i < n ; i ++ )
                for( int j = 0 ; f && j < m ; j ++ ){
                    if( a[i][j] != b[(i+r)%n][ (j+c) % m ] ) f = 0; 
                }
            if( f ) cout << "Yes\n" , exit(0); 
        }
    cout << "No\n";
    return 0;
}

C - Cross

暴力枚举点的坐标,暴力枚举大小,然后暴力判断即可。

#include<bits/stdc++.h>

using namespace std;

const int dx[] = {1, 1, -1, -1};
const int dy[] = {-1, 1, -1, 1};

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<string> g(n);
    vector<int> cnt(min(n, m) + 1);
    for (auto &i: g) cin >> i;

    for (int i = 0, t; i < n; i++)
        for (int j = 0; j < m; j++) {
            if (g[i][j] == '.') continue;
            t = 0;
            for (int k = 1, f; k; k++) {
                f = 1;
                for (int l = 0; l < 4 && f; l++)
                    if (g[i + dx[l] * k][j + dy[l] * k] == '.') f = 0;
                if (f) t = k;
                else break;
            }
            cnt[t]++;
        }
    for (int i = 1; i < cnt.size(); i++) cout << cnt[i] << " ";
    return 0;
}

D - AABCC

首先求一下素数,然后枚举\(a,b\)二分\(c\)就好了。

#include<bits/stdc++.h>

using namespace std;

#define int __int128
typedef long long ll;


int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    const int N = 300000;
    vector<int> not_prime(N + 1, 0);
    vector<int> prime;
    not_prime[0] = not_prime[1] = true;
    for (int i = 2; i <= N; i++) {
        if (not_prime[i]) continue;
        prime.push_back(i);
        for (int j = i * 2; j <= N; j += i)
            not_prime[j] = 1;
    }
    int n = read(), res = 0;
    for (int i = 0, a, b; i < prime.size(); i++) {
        a = prime[i] * prime[i];
        if (a > n) break;
        for (int j = i + 1; j < prime.size(); j++) {
            b = prime[j] * a;
            if (b > n) break;
            int l = j + 1, r = prime.size() - 1, mid, ans = -1;
            while (l <= r) {
                mid = (l + r) >> 1;
                if (b * prime[mid] * prime[mid] <= n) ans = mid, l = mid + 1;
                else r = mid - 1;
            }
            if (ans == -1) break;
            res += ans - j;
        }

    }
    cout << (ll) res << "\n";
    return 0;

}

E - Dice Product 3

有题可知

\(f(i)=\frac 1 6(f(i)+f(\frac i 2 ) + f(\frac i 3 ) + f(\frac i 4) + f(\frac i 5) + f(\frac i 6) )\)

\(\frac 5 6f(i)=\frac 1 6(f(\frac i 2 ) + f(\frac i 3 ) + f(\frac i 4) + f(\frac i 5) + f(\frac i 6) )\)

\(f(i)=\frac 1 5(f(\frac i 2 ) + f(\frac i 3 ) + f(\frac i 4) + f(\frac i 5) + f(\frac i 6) )\)

其中\(f(1)=1\),如果不能整除的话就是\(0\)

然后直接记忆化搜索一下?

#include<bits/stdc++.h>

using namespace std;

#define int long long

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

const int mod = 998244353, inv = 598946612;

map<int, int> f;

int dp(int n) {
    if (f.count(n)) return f[n];
    int ans = 0;
    for (int i = 2; i <= 6; i++)
        if (n % i == 0) ans = (ans + dp(n / i)) % mod;
    return f[n] = ans * inv % mod;
}

int32_t main() {
    int n;
    cin >> n;
    f[1] = 1;
    cout << dp(n);
    return 0;
}

F - More Holidays

记录\(S\)中每一个x的位置,很容易可以想到相邻的x一定是最优的,所以直接枚举一下起点就好。但是枚举的是时候实际上只用枚举第一个\(S\)中的位置就可以。然后特判第一个和最后一个的特殊情况。

#include <bits/stdc++.h>

using namespace std;

#define int long long

int32_t main(){
	ios::sync_with_stdio(false) , cin.tie(nullptr) , cout.tie(nullptr);
	int n , m , k;
	string s;
	cin >> n >> m >> k >> s;
	vector<int> p;
	for( int i = 0 ; i < n ; i ++ )
		if( s[i] == 'x' ) p.push_back(i);
	int cnt = p.size() , t = cnt * m;
	if( cnt * m == k ) cout << n * m , exit(0);
	int res = 0;
	res = max( res , n * ( k / cnt ) + p[k%cnt] );
	res = max( res , n * ( k / cnt ) + n - 1 - p[cnt - 1 - k % cnt] );
	for( int i = 0 ; i < cnt ; i ++ ){
		if( k + i + 1 < t )
			res = max( res , p[(k+i+1)%cnt] + n * ((k+i+1) / cnt) - p[i] - 1 );
	}
	cout << res;
	return 0; 
}

标签:prime,AtCoder,ch,frac,Beginner,300,++,int,&&
From: https://www.cnblogs.com/PHarr/p/17369742.html

相关文章

  • AtCoder Regular Contest 128 D Neq Neq
    洛谷传送门AtCoder传送门考虑把所有\(a_i=a_{i+1}\)的位置断开,分别计算然后把方案数乘起来。接下来的讨论假设\(a_i\nea_{i+1}\)。考虑一个dp,设\(f_i\)为\([1,i]\)最后剩下的集合的方案数。转移显然是\(f_i\getsf_i+f_j\),但是需要满足\((a_j,a_{j+1},...,......
  • AtCoder Beginner Contest 242(D,E)
    AtCoderBeginnerContest242(D,E)D(二叉树搜索)D题目大意就是首先给你一个字符串,代表\(S^0\),然后我们可以操作得到\(S^1,S^2\)等等我们可以知道\(S^i\)是拿\(S^(i-1)\)经过一系列替换而来的,因为这个字符串只有三种字符串,\(A,B,C\),这个替换方式就是把\(A\)替换成\(BC\),把\(B\)......
  • 【题解】ABC300 F,G
    F.MoreHolidays题目分析:考虑刻画一下我们选择是什么样子的。考虑我们最后选择的\(T\)中的一段一定是形如:一个完整的S选择一个后缀\(+\)若干个完整的S\(+\)一个完整的S的前缀。这样的话就启示我们直接枚举这个前后缀选择的是什么,然后就可以很快算出来了,但是枚举前......
  • AtCoder Regular Contest 125 F Tree Degree Subset Sum
    洛谷传送门AtCoder传送门首先将度数\(-1\)。设\(f_i\)为体积为\(i\)至多能用几个物品凑出来,\(g_i\)为至少。我们现在要证明一个东西:\(x\in[g_i,f_i]\),\((i,x)\)合法。首先若\((s,x)\)合法,那么必须满足\(s-x\in[-z,z-2]\),其中\(z=\sum\limits_{i=1}......
  • ABC300E Dice Product 3
    题意初始一个整数为\(1\),你有一个可以等概率地掷出\(1\)至\(6\)这六个数值的骰子,再给定一个整数\(N\),当初始给出的整数严格小于\(N\)时,重复以下操作:掷一次骰子,将初始给出的整数乘上你掷出来的数字。求出最终得到\(N\)的概率模上\(998244353\)的值。思路先判无......
  • AT_abc106_d [ABC106D] AtCoder Express 2 题解
    题目传送门解题思路区间\(dp\)。划分阶段:以左右城市之间的列车数量为阶段。状态表达:设\(f_{i,j}\)为城市\(i\)与城市\(j\)之间的列车数量。状态转移:由图可知,城市\(l\)与城市\(r\)之间的列车数量,就是城市\(l\)与城市\(r-1\)之间的列车数量与城市\(l+1\)与......
  • AtCoder Regular Contest 119 F AtCoder Express 3
    洛谷传送门AtCoder传送门很厉害的题!考虑所有车站已确定,如何求\(0\)到\(n+1\)的最短路。设\(g_{i,0}\)为只考虑\(0\simi\)的点,到\(i\)和它左边第一个\(\text{A}\)的最短路,\(g_{i,1}\)同理。有转移:若\(s_{i-1}=\text{A},s_i=\text{A},g_{i,0}\getsg_{......
  • AtCoder Regular Contest 119 D Grid Repainting 3
    洛谷传送门AtCoder传送门对每个红格的行和列连边,建出二分图。对于二分图中的每个连通块分别考虑。大胆猜测对于每个连通块,我们都能够进行适当的操作,使得只有一行/一列没被操作(显然不能使所有行和列都被操作)。对应的方案就是随便取一棵生成树,把不被染白的那一行/列拎出来当根,然......
  • AtCoder Regular Contest 117 F Gateau
    洛谷传送门AtCoder传送门差分约束算法:给出\(m\)个不等式形如\(x_{a_i}\lex_{b_i}+y_i\),求是否有解。考虑把不等式看成图上的三角不等式\(dis_v\ledis_u+d\),连边\((b_i,a_i,y_i)\),以\(x_i\)最大的位置跑最短路,如果图中有负环就无解。此时求出来的\(dis_i\)......
  • Atcoder Grand Contest 059 E - Grid 3-coloring(转化+思维)
    首先先是一步很猛的操作——将三染色视作构造一个矩阵使得相邻元素相差\(1\)且每个元素\(\bmod3\)的值就等于其颜色。证明是显然的,我们按从上到下从左到右的顺序填数,可以归纳证明,对于一个相邻格子颜色互不相同的矩阵的填数方案,处于斜对角的两个格子上写的数要么差\(2\),要么......