跳到正文

个人记录

代码源 Day 3 总结

CuteGirlGC约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟

版本

  1. v1当前

省流:不开 long long 见祖宗。

A

WA*1

比较简单,首先推式子然后直接暴力查找就行了,错因是忘记开 long long。

C++
#include <iostream>
#include <cmath>

using namespace std;
using ll = long long;

ll n;

ll S(ll x) {
  ll c = 0;
  while (x) {
    c += x % 10;
    x /= 10;
  }
  return c;
}

ll G(ll x) {
  return x * x + x * S(x);
}

int main() {
  cin >> n;
  for (ll i = max(0ll, (ll)sqrt(n) - 81); i <= sqrt(n); i++) {
    if (G(i) == n) {
      cout << i << '\n';
      return 0;
    }
  }
  cout << -1 << "\n";
  return 0;
}

B

CE*1

错因是 Check 中出现了两个一样的变量,但是 CE 不算次数:)

最好就是一种金字塔的结构。

C++
#include <iostream>

using namespace std;
using ll = long long;

ll n, m, k;

ll S(ll l, ll r) {
  return (l + r) * (r - l + 1) / 2;
}

bool C(ll x) {
  ll s = x, i = k - 1, j = x - 1;
  if (j >= i) {
    s += S(j - i + 1, j);
  } else {
    s += S(1, j) + (i - j);
  }
  i = n - k, j = x - 1;
  if (j >= i) {
    s += S(j - i + 1, j);
  } else {
    s += S(1, j) + (i - j);
  }
  return s <= m;
}

int main() {
  cin >> n >> m >> k;
  ll l = 1, r = m - n + 1, ans;
  while (l <= r) {
    ll m = l + r >> 1;
    if (C(m)) {
      l = m + 1, ans = m;
    } else {
      r = m - 1;
    }
  }
  cout << ans << '\n';
  return 0;
}

C

WA*1

二分时间,算能够打印出来的书本数量和是否大于 kk。

错因:long long

C++
#include <iostream>

using namespace std;
using ll = long long;

const ll kMaxN = 2e5 + 1;

ll t, n, k;
struct Node {
  ll t, l, w, o;
} a[kMaxN];

bool C(ll x) {
  ll r = 0;
  for (ll i = 1; i <= n; i++) {
    ll tmp1 = x / a[i].o, tmp2 = x % a[i].o;
    r += tmp1 * a[i].l + min(tmp2 / a[i].t, a[i].l);
    if (r >= k) {
      return 1;
    }
  }
  return r >= k;
}

int main() {
  for (cin >> t; t; t--) {
    cin >> n >> k;
    for (ll i = 1; i <= n; i++) {
      cin >> a[i].t >> a[i].l >> a[i].w, a[i].o = a[i].l * a[i].t + a[i].w;
    }
    ll l = 1, r = 2e18, ans;
    while (l <= r) {
      ll m = l + r >> 1;
      if (C(m)) {
        ans = m, r = m - 1;
      } else {
        l = m + 1;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}

D

一遍 AC。

首先,小学数学,没了。

C++
#include <iomanip>
#include <iostream>

using namespace std;
using ll = long long;

double k, d, t;

int main() {
  cin >> k >> d >> t;
  ll n = (k - 1) / d + 1;
  double x = n * d;
  ll m = t * 2 / (x + k);
  double tmp1 = t * 2 - (x + k) * m, tmp2 = x * m;
  if (tmp1 <= k * 2 - 1) {
    tmp2 += tmp1 / 2;
  } else {
    tmp2 += k, tmp2 += tmp1 - k * 2;
  }
  cout << fixed << setprecision(10) << tmp2;
  return 0;
}

E

1遍AC

还是感觉有点难度的,做了蛮久,做法是二分答案能够升级的层数。

C++
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

const ll kMaxN = 2e5 + 1;

ll n, k, a[kMaxN], t[kMaxN], c[kMaxN], need[kMaxN];

bool C(ll x) {
  for (ll i = 1; i <= k; i++) {
    need[i] = a[i];
  }
  for (ll i = 1; i <= x; i++) {
    need[t[i]] -= c[i];
  }
  vector<ll> s1, s2;
  for (ll i = 1; i <= k; i++) {
    if (need[i] < 0) {
      s1.push_back(-need[i]);
    } else if (need[i] > 0) {
      s2.push_back(need[i]);
    }
  }
  while (s1.size() && s2.size()) {
    auto x = s1.back(), y = s2.back();
    s1.pop_back(), s2.pop_back();
    if (3 * x > y) {
      s1.push_back(x - y / 3);
    } else {
      y -= 3 * x;
      if (y >= 3) {
        s2.push_back(y);
      }
    }
  }
  return s1.empty();
}

int main() {
  cin >> n >> k;
  for (ll i = 1; i <= k; i++) {
    cin >> a[i];
  }
  for (ll i = 1; i <= n; i++) {
    cin >> t[i] >> c[i];
  }
  ll l = 0, r = n + 1;
  while (l < r - 1) {
    ll m = l + r >> 1;
    if (C(m)) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << l << '\n';
  return 0;
}

I

1遍AC。

做法是二分年份,然后 Check 就是求平年数量是否大于 kk。注意算的过程中可能会爆 long long,所以要用 __int128。而且要加上 15331533,就是 0∼20240\sim2024 的闰年数量。

C++
#include <iostream>

using namespace std;
using ll = long long;
using i128 = __int128;

ll t, n, k;

bool C(i128 x) {
  i128 s = 0;
  for (i128 i = 4; i <= x; i *= 100) {
    s += x / i - x / (25 * i);
  }
  return x - s >= k;
}

int main() {
  for (cin >> t; t; t--) {
    cin >> k, k += 1533;
    ll l = 1, r = 2e18;
    while (l <= r) {
      ll m = l + r >> 1;
      if (C(m)) {
        r = m - 1;
      } else {
        l = m + 1;
      }
    }
    cout << l << '\n';
  }
  return 0;
}
原文