1. 程式人生 > >【CodeForces】Lyft Level 5 Challenge 2018 - Elimination Round (Div. 1 + Div. 2) 題解

【CodeForces】Lyft Level 5 Challenge 2018 - Elimination Round (Div. 1 + Div. 2) 題解

【比賽連結】

【題解連結】

**【A】**King Escape

【思路要點】

  • 皇后會攻擊到 8 8 條直線,其中 4
    4
    條斜向的可以跨過,因此可以忽略。
  • 判斷起始點和目標點是否在其餘 4 4 條線分割出的同一個聯通塊內即可。
  • 時間複雜度 O
    ( 1 ) O(1)

【程式碼】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef
long double ld; typedef unsigned long long ull; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } int n, ax, ay, bx, by, cx, cy; int f(int x, int y) { int ans = 0; ans += x < ax; ans *= 2; ans += y < ay; return ans; } int main() { read(n); read(ax), read(ay); read(bx), read(by); read(cx), read(cy); if (f(bx, by) == f(cx ,cy)) printf("YES\n"); else printf("NO\n"); return 0; }

**【B】**Square Difference

【思路要點】

  • 由題,我們要判斷 a 2 b 2 a^2-b^2 是否是質數。
  • 由於 a 2 b 2 = ( a + b ) ( a b ) a^2-b^2=(a+b)(a-b) ,若 a b &gt; 1 a-b&gt;1 a 2 b 2 a^2-b^2 顯然不是質數。
  • 否則則判斷 a + b a+b 是否為質數即可。
  • 時間複雜度 O ( a + b ) O(\sqrt{a+b})

【程式碼】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
bool prime(ll x) {
	for (int i = 2; 1ll * i * i <= x; i++)
		if (x % i == 0) return false;
	return true;
}
int main() {
	int T; read(T);
	while (T--) {
		ll a, b; read(a), read(b);
		if (a == b + 1 && prime(a + b)) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

**【C】**Permutation Game

【思路要點】

  • 按照權值從大到小暴力博弈 d p dp 即可。
  • 時間複雜度 O ( N L o g N ) O(NLogN)

【程式碼】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int n, a[MAXN], p[MAXN];
bool dp[MAXN];
bool cmp(int x, int y) {
	return a[x] > a[y];
}
int main() {
	read(n);
	for (int i = 1; i <= n; i++)
		read(a[i]), p[i] = i;
	sort(p + 1, p + n + 1, cmp);
	for (int i = 1; i <= n; i++) {
		int pos = p[i];
		for (int j = pos + a[pos]; j <= n; j += a[pos])
			if (a[j] > a[pos]) dp[pos] |= !dp[j];
		for (int j = pos - a[pos]; j >= 1; j -= a[pos])
			if (a[j] > a[pos]) dp[pos] |= !dp[j];
	}
	for (int i = 1; i <= n; i++)
		if (dp[i]) putchar('A');
		else putchar('B');
	putchar('\n');
	return 0;
}

**【D】**Divisors

【思路要點】

  • 3 3 5 5 個因數的數應當形如 p 2 , p 3 , p 4 , p q p^2,p^3,p^4,pq ,其中 p , q p,q 為質數。
  • 其中形如 p 2 , p 3 , p 4 p^2,p^3,p^4 的較容易分解,可以先用 p o w pow 函式估算方根,再微調以消除誤差,即可檢查該數是否為完全 4 , 3 , 2 4,3,2 次方數,從而分解該數。
  • 剩餘的形如 p q pq 的數可以通過計算 g c d gcd 進行試探分解,若仍然無法分解,則說明該數對應的 p , q p,q 不在不與其相等的數中出現,因此單獨計算對答案的貢獻即可,即若有 x x 個該數,則貢獻為 ( x + 1