牛客周賽47 (待補F和思路)

薛定谔的AC發表於2024-06-16

比賽連結:牛客周賽47


賽時感受

又是一場思維題,應該只有EF有點演算法,E需要使用快速冪和取餘,F做不出,C卡了我一下,D寫完了,E不寫完一半又回來看C才做掉的,E也卡了很久雖然鴿巢原理想到了,但是沒想到被卡在取餘問題上,一開始沒想出來,去做F然後做了半個小時發現做不掉,又回來在E上做功夫。


A

思路

程式碼

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;

ll n[N];
int main() {
  int a, b, c, d, e;
  cin >> n[1] >> n[2] >> n[3] >> n[4] >> n[5];

  sort(n + 1, n + 1 + 5);
  if ((n[3] == n[5] && n[1] == n[2]) || (n[1] == n[3] && n[4] == n[5])) cout << "YES" << endl;
  else cout << "NO" << endl;

  return 0;
}

B

思路

程式碼

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;

ll n;
int main() {
  cin >> n;

  string s[N];
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < 26; i++) {
    int flag = 0;
    for (int j = 1; j <= n; j++) {
      if (s[j].find('a' + i) == -1) {
        flag = 1;
        break;
      }
    }
    if (flag)
      continue;
    cout << (char)('a' + i);
    break;
  }  

  return 0;
}

C

思路

程式碼

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;

ll n, a[N], sum, maxn;
int main() {
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
    maxn = max(maxn, a[i]);
  }

  if (sum % 2 == 0) {
    if (n == 2) {
      if (a[1] == a[2]) {
        cout << 0 << endl;
      } else {
        cout << 1 << endl;
      }
    } else if (sum <= maxn * 2 || (sum - maxn == n - 1)) {
      cout << 1 << endl;
    } else if (maxn == 1) {
      cout << 0 << endl;
    } else {
      cout << n << endl;
    }
  } else {
    if (sum <= maxn * 2 && maxn != 1) {
      cout << 1 << endl;
    } else {
      cout << n << endl;
    }
  }

  return 0;
}

D

思路

程式碼

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;

ll n, a[N] = { 1, 2, 4, 5, 7, 8, 10, 11, 14, 16, 17, 19, 20, 22, 25, 26, 28, 29 };
int main() {
  int t;
  cin >> t;
  while (t--) {
    ll n;
    cin >> n;
    cout << ((n - 1) / 18) * 30 + a[((n - 1) % 18)] << endl;
  }

  return 0;
}

E

思路

分類討論橫軸對稱,縱軸對稱和中心堆成,結果為橫軸對稱 + 縱軸堆成 - 中心對稱,因為中心堆成的部分都包含於橫軸對稱和縱軸堆成,若不減去則會計算兩次。
縱軸對稱:n為奇數時,縱軸所在的元素只能選擇0,8,縱軸左邊的元素只能選擇0,8,2,5,當左邊選擇0,8時右邊對應選擇0,8,左邊選擇2,5時,右邊對應選擇5,2,所以n為奇數時縱軸對稱的情況種數為:((pow(4, n / 2) * 2)。n為偶數時,縱軸左邊的元素和右邊元素的選擇情況和n為奇數時一致,只是不需要考慮縱軸所在的元素的選擇情況,所以n為偶數時縱軸對稱的情況種數為:(pow(4, n / 2) * 1),綜合討論得到縱軸對稱的種數為:((pow(4, n / 2) * (n % 2 == 1 ? 2 : 1))
橫軸對稱:n不用考慮奇偶性,計算器上能顯示的數字為1,3,8,0,所以橫軸對稱的情況種數為:pow(4, n)
中心對稱:中心對稱時,計算器上顯示的數字只能是0,8,所以會被橫軸對稱和縱軸對稱都計算了一遍。但是同時又需要是橫軸對稱又要是縱軸對稱,所以中心對稱的情況種數為:pow(2, ((n + 1) / 2))

程式碼

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
const ll mod = 1e9 + 7;
ll pow(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1) {
      res *= x;
      res %= mod;
    }
    y >>= 1;
    x *= x;
    x %= mod;
  }
  return res;
}
int main() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 4 << endl;
    return 0;
  }
  ll res = (((pow(4ll, n / 2) * (n % 2 == 1 ? 2ll : 1ll)) % mod + pow(4ll, n)) % mod - pow(2ll, ((n + 1) / 2)) + mod);
  cout << (res % mod);
  return 0;
}

F

思路

程式碼


相關文章