ABC305 CDE Solution - By LaDeX

GIAOchen 2023-07-26 18:24:27 3


大水题。 按题意模拟。

const LL N = 1e3 + 10;
LL n, m;
string str[N];
int main(){
	Fcin;
	cin >> n >> m;
	for (LL i = 1; i <= n; i ++){
		cin >> str[i];
		for (LL j = 0; j < m - 1; j ++){
			if (str[i][j] == str[i][j + 1] && str[i][j] == 'T')
				str[i][j] = 'P', str[i][j + 1] = 'C';
		}
		cout << str[i] << "\n";
	}
	return 0;
}

题意即为辗转相减法求最大公因数。

用辗转相除法加速。

LL A, B, Ans = 0;
 
LL GCD(LL x, LL y){
	if (x == y)
		return -1;
	if (x % y == 0){
		Ans += x / y - 1;
		return y;
	}
	Ans += x / y;
	return GCD(y, x % y);
}
 
int main(){
	Fcin;
	cin >> A >> B;
	GCD(A, B);
	cout << Ans;
	return 0;
}

n 很小,用 set 处理,对于每个物品处理前 大。

时间复杂度

set<LL> s;
LL n, K;
 
int main(){
	Fcin;
	cin >> n >> K;
	LL x;
	s.insert(0);
	for (LL i = 1; i <= n; i ++){
		cin >> x;
		auto it = s.begin();
		for (LL j = 1; j <= K; j ++){
			s.insert(*it + x);
			++ it;
		}
	}
	auto it = s.begin();
	for (LL j = 1; j <= K; j ++){
		++ it;
	}
	cout << *it << " ";
	return 0;
}

{{ vote && vote.total.up }}