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

AtCoder Beginner Contest 293

时间:2023-03-19 21:23:06浏览次数:48  
标签:std AtCoder ch return Beginner int sum vector 293

A - Swap Odd and Even

#include<bits/stdc++.h>
using namespace std;

int32_t main(){
	string s;
	cin >> s;
	for( int i = 0 ; i + 1 < s.size() ; i += 2 )
		swap( s[i] , s[i+1] );
	cout << s;
	return 0;
}

B - Call the ID Number

这道题的难点感觉是阅读。题意是每一个人\(i\)会叫一个人\(A_i\),从\(1\)开始,如果这个人被叫过,他就不会叫人,反之他就会叫人。输出所有没有被叫过的人。

#include<bits/stdc++.h>
using namespace std;

int32_t main(){
	int n;
	scanf("%d" , &n );
	vector<int> a(n+1);
	for( int i = 1 ; i <= n ; i ++ )
		scanf("%d",&a[i]);
	vector<bool> vis(n+1,false);
	for( int i = 1 ; i <= n ; i ++ ){
		if( vis[i] ) continue;
		vis[ a[i] ] = true;
	}
	vector<int> k;
	for( int i = 1 ; i <= n ; i ++ ){
		if( vis[i] == false ) k.push_back(i);
	}
	printf("%d\n" ,k.size());
	for( int i : k ) printf("%d " , i );
	return 0;
}

C - Make Takahashi Happy

数据范围很小,直接 dfs 搜索就行,搜索的过程中记录一下经过路径上的值。

#include<bits/stdc++.h>
using namespace std;

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

int n , cnt = 0 , m ;
vector<vector<int>> a;

void dfs( int x , int y , set<int> st ){
	if( st.insert( a[x][y] ).second == false ) return;
	if( x == n && y == m ){
		cnt ++;
		return ;
	}
	if( x+1 <= n ) dfs( x+1 , y , st );
	if( y+1 <= m ) dfs( x , y+1 , st );
	return;
}

int32_t main(){
	n = read() , m = read();
	a = vector<vector<int>>(n+1, vector<int>(m+1,0) );
	for( int i = 1 ; i <= n ; i ++ )
		for( int j = 1 ; j <= m ; j ++ )
			a[i][j] = read();
	dfs( 1 , 1 , set<int>() );
	cout << cnt;
	return 0;
}

D - Tying Rope

要照射到所有的位置就要保证向左照最靠右和向右照最靠左的相邻或相交才行。

#include<bits/stdc++.h>
using namespace std;

bool flag;
vector<vector<int>> e;
int n , m;
vector<int> vis , R , B ;

void dfs( int x ){
	vis[x] = 1;
	if( R[x] == 0 || B[x] == 0 ) flag = false;
	for( auto v : e[x] ){
		if( vis[v] ) continue;
		dfs(v);
	}
	return;
}

int32_t main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0); 
	cout.tie(0);

	cin >> n >> m;
	e = vector<vector<int>>(n+1 , vector<int>() );
	vis = vector<int>(n+1) , R = vector<int>(n+1,0) , B = vector<int>(n+1,0);
	int a , c;
	char b , d;
	for( ; m ; m -- ){
		cin >> a >> b >> c >> d;
		e[a].push_back(c) , e[c].push_back(a);
		if( b == 'R' ) R[a] = 1;
		else B[a] = 1;
		if( d == 'R' ) R[c] = 1;
		else B[c] = 1;
	}
	int x = 0 , y = 0;
	for( int i = 1 ; i <= n ; i ++ ){
		if( vis[i] ) continue;
		flag = true;
		dfs( i );
		if( flag ) x ++;
		else y ++;
	}
	cout << x << " " << y;
	return 0;
}

E - Geometric Progression

令\(\sum_{i=0}^{x-1} A^i=sum(x)\)

如果\(x\)是偶数

\[sum(x)=a^0+a^1+\cdots+a^{\frac x2-1}+a^{\frac x2}(a^0+a^1+\cdots+a^{\frac x2-1})=(1+\frac x2)sum(x/2) \]

如果\(x\)是奇数

\[sum(x)=a^0+a(a^0+a^1+a^{x-2})=1+a\times sum(x-1) \]

快速幂加递归即可,复杂度\(O(\log^2(N))\)

#include<bits/stdc++.h>

using namespace std;

#define int long long

int a, x, m;

int power(int X, int Y) {
    int ans = 1;
    while (Y) {
        if (Y & 1) ans = ans * X % m;
        X = X * X % m, Y >>= 1;
    }
    return ans % m;
}

int sum(int x) {
    if (x == 1) return 1;
    if (x & 1) return (1 + a * sum(x - 1) ) % m;
    return ((power(a, x / 2) + 1) % m * sum(x / 2) % m) % m;
}

int32_t main() {
    cin >> a >> x >> m;
    cout << sum(x)%m << "\n";
    return 0;
}

标签:std,AtCoder,ch,return,Beginner,int,sum,vector,293
From: https://www.cnblogs.com/PHarr/p/17234336.html

相关文章

  • 【题解】ABC293E Sol
    题目大意给定整数\(A,X,M\),求\(\sum\limits^{X-1}_{i=0}A^i\)对\(M\)取模的值。数据范围:\(1\leA,M\le10^9\),\(1\leX\le10^{12}\)。题目分析直接算显然......
  • AtCoder Beginner Contest 293
    上周因为GDKOI咕咕咕了A-SwapOddandEven(abc293a)题目大意给定一个字符串,交换每两个相邻字母,输出结果。解题思路模拟即可。神奇的代码#include<bits/std......
  • AtCoder Beginner Contest 293(C,D ,E,F)
    AtCoderBeginnerContest293(C,D,E,F)CC这道题其实还蛮好写的,就是一个\(dfs\),然后我看错了题意,就记录一下这道题的大意是我们需要从\((1,1)\)走到\((n,m)\),我们只......
  • [AtCoder Beginner Contest 281][G. Farthest City]
    和CF1657E的做法十分相似题目链接:G-FarthestCity题目大意:问有多少个\(n(3\len\le500)\)个点的无向连通图满足,若设\(1\)到\(i\)的最短路距离为\(dis_i\),则......
  • AtCoder Regular Contest 158
    Preface这场比赛刚好周日晚上没事就打了,堪堪混过三道题,也算是小上了一波分吧但是由于A题脑抽了一波卡了30min,导致排名不高,也没时间看DE了,属实有点可惜A-+3+5+7显......
  • AtCoder Beginner Contest 293
    题解报告基本的一些理解和问题都在注释中A:SwapOddandEven//水题#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>usingnamespace......
  • AtCoder Beginner Contest 240 F
    ABC240F思路维护前缀和B,以及B的前缀和C,然后在每次添加连续y个x的时候,从中找出最大的\(C_i\)(用pre记录),更新答案。有四种情况\(B\geq0,x>0\)那么新出现的\(C_i\)中......
  • atcoder ABC
    Ex-OptimalPathDecomposition题目只能给链染色,问你最短的(两点距离最大值),距离为不同颜色个数f[u],g[u],f表示u可以和father同一个颜色,g表示不可以。转移记录三个值。......
  • ABC 293 ABCD(并查集)
    A-SwapOddandEven#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;typedefpair<LL,LL>PII;constLLMAXN=1e18,MINN=-1e18;constLLN=1e6......
  • ATABC293D Tying Rope
    ATABC293DTyingRope题意有\(N\)根一端涂成红色,另一端涂成蓝色的绳子,现进行\(M\)次操作,第\(i\)次操作给出两个整数\(A_i\),\(C_i\)与两个字符\(B_i\),\(D_i\),表......