【CodeForces】Lyft Level 5 Challenge 2018 - Elimination Round (Div. 1 + Div. 2) 題解
阿新 • • 發佈:2018-11-08
【比賽連結】
【題解連結】
**【A】**King Escape
【思路要點】
- 皇后會攻擊到 條直線,其中 條斜向的可以跨過,因此可以忽略。
- 判斷起始點和目標點是否在其餘 條線分割出的同一個聯通塊內即可。
- 時間複雜度 。
【程式碼】
#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
【思路要點】
- 由題,我們要判斷 是否是質數。
- 由於 ,若 , 顯然不是質數。
- 否則則判斷 是否為質數即可。
- 時間複雜度 。
【程式碼】
#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
【思路要點】
- 按照權值從大到小暴力博弈 即可。
- 時間複雜度 。
【程式碼】
#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
【思路要點】
- 有 至 個因數的數應當形如 ,其中 為質數。
- 其中形如 的較容易分解,可以先用 函式估算方根,再微調以消除誤差,即可檢查該數是否為完全 次方數,從而分解該數。
- 剩餘的形如 的數可以通過計算 進行試探分解,若仍然無法分解,則說明該數對應的 不在不與其相等的數中出現,因此單獨計算對答案的貢獻即可,即若有 個該數,則貢獻為