ABC 293题解

A - Swap Odd and Even

题目传送门

题目思路

主要考察字符串
其实只用枚举每两个点然后交换一下这两个字符的特点

代码

#include <iostream>

using namespace std;

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

B - Call the ID Number

题目传送门

题目思路

主要考察数组的基本用法
这道题可以用桶来记录一下这个人是否在接电话,最后统计一下有多少个人即可

代码

#include <iostream>

using namespace std;

const int maxN = 2e5 + 5;
bool vis[maxN];
int a[maxN];

int main(){
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        if(vis[i]){
            continue;
        }
        vis[a[i]] = true;
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            ans++;
        }
    }
    cout << ans << endl;
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            cout << i << ' ';
        }
    }
}

C - Make Takahashi Happy

题目传送门

题目思路

主要考察深度优先搜索
如果我们看一下数据范围就可以发现 2 ≤ H , W ≤ 10 2 \leq H, W \leq 10 2H,W10 这里的 H H H W W W 很小,因此我们可以考虑深度优先搜索

代码

#include <iostream>
#include <set>

using namespace std;

const int maxN = 15;
int a[maxN][maxN], ans;
set<int> S;
int n, m;

void dfs(int x, int y){
    if(x < 1 || y < 1 || x > n || y > m || S.count(a[x][y])){
        return;
    }
    if(x == n && y == m){
        ans++;
        return;
    }
    S.insert(a[x][y]);
    dfs(x + 1, y);
    dfs(x, y +1);
    S.erase(a[x][y]);
}

int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> a[i][j];
        }
    }
    dfs(1, 1);
    cout << ans << endl;
    return 0;
}

D - Tying Rope

题目传送门

题目思路

主要考察并查集
由于此题基本上就是板子题,在这里就不多说了

代码

#include <iostream>
#include <cstring>

using namespace std;

int fa[200005];
bool ans[200005];

int root(int x) {
	if (fa[x] == -1) {
		return x;
	}
	return fa[x] = root(fa[x]);
}

void merge(int x, int y) {
	int a = root(x), b = root(y);
	if (a != b) {
		fa[a] = b;
	}
}

int graph[200005][2];

int main() {
	memset(fa, -1, sizeof(fa));
	memset(graph, -1, sizeof (graph));
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < m; i++) {
		int a, c;
		string b, d;
		cin >> a >> b >> c >> d;
		a--, c--;
		merge(a, c);
		if (b == "R") {
			graph[a][0] = c;
		} else {
			graph[a][1] = c;
		}
		if (d == "R") {
			graph[c][0] = a;
		} else {
			graph[c][1] = a;
		}
	}
	for (int i = 0; i < n; i++) {
		if (graph[i][0] == -1 || graph[i][1] == -1) {
			ans[root(i)] = 1;
		}
	}
	int x = 0, y = 0;
	for (int i = 0; i < n; i++) {
		if (root(i) == i) {
			x += !ans[i];
			y += ans[i];
		}
	}
	printf("%d %d", x, y);
	return 0;
}

E - Geometric Progression

题目传送门

题目思路

主要考察对等比数列的求和公式
其实如果你看了题目的样例你就会发现其实是求一个比相同的数列的

代码

#include<bits/stdc++.h>
using namespace std;
typedef __int128 LL;
typedef long long ll;
ll a,x,m;
LL p;
LL qpow(LL x,LL y){
	LL re=1;
	for(;y>0;y>>=1){
		if(y&1)re=re*x%p;
		x=x*x%p;
	}
	return !re?p:re;
}
int main(){
    cin>>a>>x>>m;
    if(a==1){
        cout<<x%m;
        return 0;
    }
    p=m*(a-1);
    cout<<(ll)(qpow(a,x)-1)/(a-1);
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值