webpack常用載入器、外掛總結
Codeforces Round #685 (Div. 2)
A Subtract or Divide
思路:對於偶數直接除 \(2\) 再減 \(1\) ,奇數先減 \(1\) ,然後和偶數相同,小一點的數要特判
#include <bits/stdc++.h> using namespace std; #define lc (rt << 1) #define rc ((rt << 1) | 1) #define fi first #define se second #define pb push_back #define pii pair<int, int> #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define per(i, r, l) for (int i = (r); i >= (l); --i) #define PE(i, u) for (int i = head[u]; i != -1; i = edge[i].next) typedef long long LL; const int maxn = 1e6 + 20; const int mod = 1e9 + 7; int a[maxn], n; int main(int argc, char const *argv[]) { int t; scanf("%d", &t); while(t--){ scanf("%d", &n); if(n == 1) { printf("0\n"); } else if(n == 2){ printf("1\n"); } else if(n == 3){ printf("2\n"); } else if(n % 2 == 0){ printf("2\n"); } else { printf("3\n"); } } return 0; }
B Non-Substring Subsequence
思路:其實只用看 \(le\) 的左邊有沒有和 \(s_{le}\) 相同的或者 \(ri\) 的右邊有沒有和 \(s_{ri}\) 相同的,有一個成立就是 \(YES\)
#include <bits/stdc++.h> using namespace std; #define lc (rt << 1) #define rc ((rt << 1) | 1) #define fi first #define se second #define pb push_back #define pii pair<int, int> #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define per(i, r, l) for (int i = (r); i >= (l); --i) #define PE(i, u) for (int i = head[u]; i != -1; i = edge[i].next) typedef long long LL; const int maxn = 1e6 + 20; const int mod = 1e9 + 7; int a[maxn], n, q; char s[maxn]; int sum[maxn]; int main(int argc, char const *argv[]) { int t; scanf("%d", &t); while(t--){ scanf("%d%d", &n, &q); scanf("%s", s + 1); rep(i, 1, n){ sum[i] = sum[i - 1]; if(s[i] == '1') sum[i]++; } while(q--){ int le, ri; scanf("%d%d", &le, &ri); if(s[le] == '1' && sum[le - 1]){ printf("YES\n"); continue; } if(s[le] == '0' && sum[le - 1] != le - 1){ printf("YES\n"); continue; } if(s[ri] == '0' && sum[n] - sum[ri] != n - ri){ printf("YES\n"); continue; } if(s[ri] == '1' && sum[n] - sum[ri]){ printf("YES\n"); continue; } printf("NO\n"); } } return 0; }
C String Equality
思路:統計串 \(s\) 和 \(t\) 中 \(a\) 到 \(z\) 的個數,然後從 \(a\) 開始列舉, 如果 \(nums[i] < numt[i]\) ,答案就是 \(No\) ,如果 \(nums[i] >= numt[i]\) ,那麼判斷 \(nums[i] - numt[i]\) 能不能整除 \(k\) ,若不能,答案就是 \(No\) ,否則 \(nums[i + 1] = nums[i] - numt[i]\) 。
#include <bits/stdc++.h> using namespace std; #define lc (rt << 1) #define rc ((rt << 1) | 1) #define fi first #define se second #define pb push_back #define pii pair<int, int> #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define per(i, r, l) for (int i = (r); i >= (l); --i) #define PE(i, u) for (int i = head[u]; i != -1; i = edge[i].next) typedef long long LL; const int maxn = 1e6 + 20; const int mod = 1e9 + 7; int a[maxn], n, q; char s[maxn], t[maxn]; int nums[maxn], numt[maxn]; int main(int argc, char const *argv[]) { int tt; scanf("%d", &tt); while(tt--){ scanf("%d%d", &n, &q); scanf("%s", s + 1); scanf("%s", t + 1); sort(s + 1, s + n + 1); rep(i, 0, 25) nums[i] = numt[i] = 0; rep(i, 1, n) nums[s[i] - 'a']++; rep(i, 1, n) numt[t[i] - 'a']++; int flag = 1; rep(i, 0, 25){ if(numt[i] > nums[i]){ flag = 0; break; } int res = nums[i] - numt[i]; if(res % q){ flag = 0; break; } nums[i + 1] += res; } if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
D Circle Game
思路:我們首先找到最大的 \(z\) 使得 \(zk * zk * 2 <= d^{2}\) ,然後判斷 \((k*(z + 1)) * (k*(z + 1)) + zk * zk \leq d^{2}\) ,滿不滿足,若滿足,則先手贏,否則後手贏。
證明:
\((k*(z + 1)) * (k*(z + 1)) + zk * zk \leq d^{2}\) 成立 :容易看出,無論後手玩家如何移動,先手玩家總有方法使得當前座標的 \(|x - y| == k\) ,所以先手必勝
\((k*(z + 1)) * (k*(z + 1)) + zk * zk \leq d^{2}\) 不成立:容易看出,無論先手玩家如何移動,後手玩家總能使當前座標 \(|x == y|\)
#include <bits/stdc++.h>
using namespace std;
#define lc (rt << 1)
#define rc ((rt << 1) | 1)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, r, l) for (int i = (r); i >= (l); --i)
#define PE(i, u) for (int i = head[u]; i != -1; i = edge[i].next)
typedef long long LL;
const int maxn = 1e6 + 20;
const int mod = 1e9 + 7;
int a[maxn], n, q;
char s[maxn], t[maxn];
int nums[maxn], numt[maxn];
int main(int argc, char const *argv[])
{
int tt;
scanf("%d", &tt);
while(tt--){
LL d, k;
scanf("%I64d%I64d", &d, &k);
LL cnt = 1;
for(LL i = k; i <= d; i += k){
if(i * i * 2LL <= d * d){
cnt = i / k * 2;
if((i + k) * (i + k) + i * i <= d * d){
cnt++;
}
}
}
if(cnt % 2){
printf("Ashish\n");
} else {
printf("Utkarsh\n");
}
}
return 0;
}
E1 E2 Bitwise Queries
由於直接看的 \(E2\) 所以直接講最優解
思路:我們先得到 \(a_1\) 和 \(a_2\) 到 \(a_n\) 所有數異或後的值 \(b_2, b_3, b_4, b_5 .... b_n\) ,容易想到只要確定一個數的值,就可以確定其他所有數,然後分兩種情況討論
一、存在相同的數,那麼一定存在某兩個 \(i, j\) 使得 \(b_i == b_j\) 或者 存在一個 \(i\) 使得 \(b_i == 0\) ,前者可以的得出 \(a_i == a_j\) ,後者可以得出 \(a_1 == a_i\)
然後我們將這兩個相同的數 \(AND\) 後的值就是這兩個數的值了
二、不存在相同的數,根據題目中 \(n\) 一定是 \(2\) 的次冪,並且 \(0\leq a[i] \leq n - 1\) 的條件,可以得出 \(a\) 是 \(0\) 到 \(n - 1\) 的一個排列, 那麼就一定存在一個 \(i\) 使得 \(b_i\)
等於 \(n - 1\) ,這樣的 \(a_1\) 和 \(a_i\) 有一個性質,對於二進位制上任意一位,如果 \(a_1\) 的值為 \(0\) ,那麼 \(a_i\) 一定為 \(1\) ,為 \(1\) 同理,那麼我們只要找到任意一個 \(j\) ,
讓 \(a_j\) 和 \(a_1, a_i\) 分別 \(AND\) 後得到值 \(c_1, c_i\) ,那麼 \(c_1 | c_i\) 的值就是 \(a_j\) 的值了
自己手推一下比較好理解
#include <bits/stdc++.h>
using namespace std;
#define lc (rt << 1)
#define rc ((rt << 1) | 1)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, r, l) for (int i = (r); i >= (l); --i)
#define PE(i, u) for (int i = head[u]; i != -1; i = edge[i].next)
typedef long long LL;
const int maxn = 1e6 + 20;
const int mod = 1e9 + 7;
int n;
int res[maxn], ans[maxn];
map<int, int> mp;
int main(int argc, char const *argv[])
{
scanf("%d", &n);
int flag = 1;
int id1 = -1, id2 = -1;
for(int i = 2; i <= n; i++){
printf("XOR 1 %d\n", i);
fflush(stdout);
scanf("%d", &res[i]);
if(res[i] == 0){
flag = 0;
id1 = 1, id2 = i;
}
if(mp.count(res[i]) && id1 == -1) {
id1 = mp[res[i]], id2 = i;
flag = 0;
}
mp[res[i]] = i;
}
if(flag == 0){
printf("AND %d %d\n", id1, id2);
fflush(stdout);
int x;
scanf("%d", &x);
ans[id1] = ans[id2] = x;
if(id1 != 1){
ans[1] = x ^ res[id1];
}
printf("! %d ", ans[1]);
rep(i, 2, n){
ans[i] = ans[1] ^ res[i];
printf("%d ", ans[i]);
}
} else {
id1 = id2 = -1;
for(int i = 2; i <= n; i++){
if(res[i] == n - 1){
id1 = 1, id2 = i;
break;
}
}
int id;
if(id2 == 2) id = 3;
else id = 2;
int b1, b2;
printf("AND %d %d\n", id1, id);
fflush(stdout);
scanf("%d", &b1);
printf("AND %d %d\n", id2, id);
fflush(stdout);
scanf("%d", &b2);
ans[id] = b1 | b2;
ans[1] = ans[id] ^ res[id];
printf("! %d ", ans[1]);
rep(i, 2, n){
ans[i] = ans[1] ^ res[i];
printf("%d ", ans[i]);
}
}
return 0;
}
F Nullify The Matrix
思路:我們定義 \(f(x)\) 為 \(a[i_1][j_1] \bigoplus a[i_2][j_2] .... \bigoplus a[i_k][j_k]\) 的值,其中 \(i_k + j_k == x\) ,也就是某一條斜對角線的異或和,如果存在 \(f(x)\) 不為 \(0\) ,那麼先手贏,否則後手贏, 具體證明請看官方題解,我也是賽後補的題(菜雞落淚
#include <bits/stdc++.h>
using namespace std;
#define lc (rt << 1)
#define rc ((rt << 1) | 1)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, r, l) for (int i = (r); i >= (l); --i)
#define PE(i, u) for (int i = head[u]; i != -1; i = edge[i].next)
typedef long long LL;
const int maxn = 1e6 + 20;
const int mod = 1e9 + 7;
int a[105][105];
int n, m;
int main(int argc, char const *argv[])
{
int t;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
rep(i, 1, n){
rep(j, 1, m){
scanf("%d", &a[i][j]);
}
}
int flag = 0;
rep(sum, 2, n + m){
int res = 0;
rep(i, 1, sum - 1){
if(!(i >= 1 && i <= n && sum - i >= 1 && sum - i <= m)) continue;
res ^= a[i][sum - i];
}
if(res) {
flag = 1;
break;
}
}
if(flag) printf("Ashish\n");
else printf("Jeel\n");
}
return 0;
}