Codeforces Round #830 (Div. 2) A.B

A. Bestie

题目链接:

Problem - A - Codeforces

题面:

题意:

有一个长度为n的数组,你可以对这个数组进行操作:花费n-i+1的钱,选择一个位置i,使ai变成gcd(i, ai),问使这个数组的gcd == 1需要多少钱

思路:

我们对数组的操作使越往后面花费越少,而且最后两个的花费和等于第三个,并且gcd(i, i+1)==1,gcd(1,i)==1,所以只要有一个1就可以,或者两个相邻的,那么我们只需要看看一开始gcd是否为1,如果不是,将最后一位操作了,如果还不是,考虑操作倒数第二位,若还不是就最后两位都操作

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll arr[25];
int n;
bool check(ll x){
	ll ans = 0;
	for(int i = 0; i < n; i++){
		if(i == x){
			continue;
		}
		ans = __gcd(ans, arr[i]);
	}
	ans = __gcd(ans, __gcd(arr[x], x + 1));
	return ans == 1;
}
int main(){
	int t;
	cin >> t;
	while(t--){
		
		cin >> n;
		ll ans = 0;
		for(int i = 0; i < n; i++){
			cin >> arr[i];
			ans = __gcd(ans, arr[i]);
		} 
		if(ans == 1){
			cout << 0 << endl;
		}else{
			if(n == 1){
				cout << 1 << endl;
			}else{
				if(check(n - 1)){
					cout << 1 << endl;
				}else if(check(n - 2)){
					cout << 2 << endl;
				}else{
					cout << 3 << endl;
				} 
			}
		}
		
	}
	return 0;
}

B. Ugu

题目链接:

Problem - B - Codeforces

题面:

题意:

有一个长度为n的01串,可以选择一个i,将i后面的(包括i)地方进行反转,问需要几步才能使这个串不递增

思路:

没次操作使将操作地方后面的串进行反转,我们可以记录操作次数,然后判断当前位置是否需要反转。如果操作次数是偶数次,那么当前位置小于前面那么就需要反转。奇数次的时候反之

代码:

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

int main(){
	int t;
	cin >> t;
	while(t--){
		int n;
		cin >> n;
		string s;
		cin >> s;
		int ans = 0;
		int f = 0;
		for(int i = 1; i < n; i++){
			if(s[i] < s[i - 1] && f == 0){
				f = 1;
				ans++;
			}else if(f && s[i] > s[i - 1]){
				ans++;
				f = 0;
			}
		}
		cout << ans << endl;
	}
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值