AtCoder Beginner Contest 161 題解

AtCoder Beginner Contest 161

A - ABC Swap

題意:略。ios

分析:略。c++

#include <bits/stdc++.h>
#define rep(i, a, b) for (long long i = a; i <= b; ++i)
#define mp make_pair
#define ll long long
#define SIZE 1005
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
int n, m, k;

int main() {
	io(); cin >> n >> m >> k;
	swap(n, m); swap(n, k);
	cout << n << ' ' << m << ' ' << k;
}

題意:略。spa

分析:略。code

#include <bits/stdc++.h>
#define rep(i, a, b) for (long long i = a; i <= b; ++i)
#define mp make_pair
#define ll long long
#define SIZE 1005
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
int n, m, k;
int main() {
	io(); cin >> n >> m;
	vector<double> a(n);
	double sum = 0;
	for (auto& i : a) cin >> i, sum += i;
	sum /= (4.0 * m);
	sort(a.begin(), a.end());
	reverse(a.begin(), a.end());
	rep(i, 0, (m - 1)) {
		if (a[i] < sum) {
			cout << "No";
			return 0;
		}
	}
	cout << "Yes";
}

C - Replacing Integer

題意:給定兩個正整數 \(N,K\) ,你可以進行如下操做無限次:將 \(N\) 替換爲 \(|N-K|\) 。詢問全部操做中 \(N\) 的最小值。ci

分析:隨便推導一下不難發現最小值只能是 \(min(K\%N,K-K\%N)\)字符串

#include <bits/stdc++.h>
#define rep(i, a, b) for (long long i = a; i <= b; ++i)
#define mp make_pair
#define ll long long
#define SIZE 1005
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
ll k, n;

int main() {
	io(); cin >> n >> k;
	cout << min(n % k, k - n % k);
}

D - Lunlun Number

題意:定義 \(Lunlun\ Number\) 是指相鄰每兩位數字之差不超過 \(1\) 的數字,求第 \(k\) 大的 \(Lunlun\ Number\)string

分析:假設某個 \(Lunlun\ Number\) 的末尾是 \(k\) ,那麼由它可以引出的下一個 \(Lunlun\ Number\) 只能是在末尾加上 \(k-1,k,k+1\) 這三種狀況(注意特判 \(k=0,9\) 的狀況)。而後用這個性質 \(BFS\) 便可。hash

#include <bits/stdc++.h>
#define rep(i, a, b) for (long long i = a; i <= b; ++i)
#define mp make_pair
#define ll long long
#define SIZE 1005
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
ll k;
set<string> st;

int main() {
	io(); cin >> k;
	queue<string> q;
	rep(i, 1, 9) q.push(to_string(i));
	vector<ll> vec;
	while (st.size() <= 2e5) {
		string top = q.front();
		q.pop();
		st.insert(top);
		if (top.back() != '9' && top.back() != '0') {
			char ch = top.back(); --ch;
			q.push(top + ch);
			q.push(top + top.back());
			ch += 2;
			q.push(top + ch);
		}
		else if (top.back() == '0') {
			char ch = top.back(); ++ch;
			q.push(top + top.back());
			q.push(top + ch);
		}
		else {
			char ch = top.back(); --ch;
			q.push(top + ch);
			q.push(top + top.back());
		}
	}
	for (auto i : st) vec.emplace_back((ll)stoll(i));
	sort(vec.begin(), vec.end());
	cout << vec[k - 1];
}

E - Yutori

題意\(Takahashi\) 在總共 \(N\) 天裏一共要選擇 \(K\) 個工做日,可是他每工做一天就要休息 \(C\) 天。給定一個由 \(x,o\) 構成的字符串,若 \(s_i=x\) 就表明第 \(i\) 天不能工做,詢問哪幾天是 \(Takahashi\) 沒得選擇,必須工做的。it

分析:先考慮 \(Takahashi\) 在什麼狀況下不存在沒法選擇的狀況,顯然是貪心時至少存在 \(K+1\) 個可選工做日。所以,咱們只須要考慮在貪心的狀況下只存在 \(K\) 個工做日時,這 \(K\) 個工做日的分佈,極端地考慮,咱們正反貪心兩次若是兩次都須要工做的日子就一定是 \(Takahashi\) 的工做日了。io

#include <bits/stdc++.h>
#define rep(i, a, b) for (long long i = a; i <= b; ++i)
#define mp make_pair
#define ll long long
#define SIZE 1005
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
ll n, k, c;

int main() {
	io(); cin >> n >> k >> c;
	string s; cin >> s;
	int pos = -1, res = 0;
	set<int> st, st2;
	rep(i, 0, (s.length() - 1)) {
		if (pos >= i || s[i] == 'x') continue;
		if (st.size() == k) {
			++res;
			break;
		}
		pos = i + c;
		st.insert(i);
	}
	if (res) return 0;
	pos = s.length();
	for (int i = s.length() - 1; i >= 0; --i) {
		if (pos <= i || s[i] == 'x') continue;
		if (st2.size() == k) {
			++res;
			break;
		}
		pos = i - c;
		if (st.count(i)) st2.insert(i);
	}
	if (res) return 0;
	for (auto i : st2) cout << i + 1 << '\n';
}

F - Division or Substraction

題意:給定一個正整數 \(N\) ,而後你能夠選擇一個正整數 \(K\) 並執行如下兩種操做:

  1. 若是 \(N\%K=0\) 則能夠將當前的 \(N\) 替換爲 \(\frac{N}{K}\)
  2. \(N\) 替換爲 \(N-K\)

詢問能將 \(N\) 轉化爲 \(1\) 的正整數 \(K\) 的數量。

分析:分紅兩部分考慮:一是 \(N\) 直接減去 \(N-1\) ,這種狀況下的 \(K\) 就是 \(N-1\) 的全部因子;二是除了狀況一之外的全部狀況(就是須要考慮操做 \(1\) 的狀況,狀況一咱們只考慮了操做 \(2\) ),這些狀況下的 \(K\) 不難發現一定是 \(N\) 的因子,所以咱們取出全部 \(N\) 的因子模擬便可。

這兩種狀況不存在相同的因子,由於相鄰天然數互質。

#include <bits/stdc++.h>
#define rep(i, a, b) for (long long i = a; i <= b; ++i)
#define mp make_pair
#define SIZE 2010
#define ll long long
using namespace std;
void io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
ll n;

int main() {
	io(); cin >> n;
	int ans = 1;
	for (ll i = 2; i * i <= n; ++i) {
		if (n % i == 0) {
			ll tmp = n;
			while (tmp % i == 0) tmp /= i;
			if ((tmp - 1) % i == 0) ans++;
			tmp = n;
			if (i * i == n) continue;
			ll x = n / i;
			while (tmp % x == 0) tmp /= x;
			if ((tmp - 1) % x == 0) ans++;
		}
	}
	if (--n > 1) ++ans;
	for (ll i = 2; i * i <= n; ++i) {
		if (n % i == 0) {
			ans += 2;
			if (i * i == n) --ans;
		}
	}
	cout << ans;
}
相關文章
相關標籤/搜索