1. 程式人生 > 其它 >[Codeforces]800分的水題題解合集(一)

[Codeforces]800分的水題題解合集(一)

閒來無事,寫一些大水題的題解,不知道什麼時候會有第二個合集

CF686A-Free Ice Cream

題解

一道正常的模擬題,按照題意進行模擬即可
需要注意的是在資料範圍內資料可能爆int,需要開long long

程式碼在這裡
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
signed main(){
  ios::sync_with_stdio(false);
  LL n, x, cnt = 0;
  cin >> n >> x;
  for(int i = 0; i < n; i++) {
    char ch;
    LL d;
    cin >> ch >> d;
    if(ch == '+') x += d;
    else if(d > x) ++cnt;
    else x -= d;
  }
  cout << x << " " << cnt << endl;
}

CF894A-QAQ

題解

由於題目中n的範圍僅有[1,100],可以直接使用O(n3)的暴力做法解決該問題

程式碼在這裡
#include <bits/stdc++.h>
using namespace std;
signed main(){
  ios::sync_with_stdio(false);
  string str;
  cin >> str;
  int ans = 0;
  for(int i = 0; i < str.length(); i++) {
    for(int j = i + 1; j < str.length(); j++) {
      for(int k = j + 1; k < str.length(); k++) {
        if(str[i] == 'Q' && str[j] == 'A' && str[k] == 'Q') ++ans;
      }
    }
  }
  cout << ans << endl;
}

CF822A-I'm bored with life

題解

注意到A!與B!都能被min(A,B)!整除,可以很輕鬆的證明GCD(A!, B!) = min(A, B)!

程式碼在這裡
#include <bits/stdc++.h>
using namespace std;
signed main(){
  ios::sync_with_stdio(false);
  int a, b;
  cin >> a >> b;
  long long n = min(a, b), ans = 1;
  for(int i = 2; i <= n; i++) ans *= i;
  cout << ans << endl;
}