当前位置:   article > 正文

AtCoder Beginner Contest 193 题解报告(先写 A ~ E,后面再写)

atcoder beginner contest 193

AtCoder Beginner Contest 193

https://atcoder.jp/contests/abc193/tasks

A - Discount

https://atcoder.jp/contests/abc193/tasks/abc193_a

老样子,A题还是签到题。题目要求计算折扣,要求答案的精度在 102 以上。因此我们只需要输出的时候保留的小数点位数足够多就可以达到要求,比如保留 18 位。

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main() {
  5. int a,b;
  6. cin>>a>>b;
  7. double ans=100.0*(a-b)/a;
  8. cout<<fixed<<setprecision(18)<<ans<<"\n";
  9. return 0;
  10. }

B - Play Snuke

https://atcoder.jp/contests/abc193/tasks/abc193_b

买东西,到第 i 个商店需要 a[i] 时间,价格为 p[i],每分钟库存减少一个。求最小价格。

一个简单的模拟题。难度还是在英文的理解上。

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. typedef long long ll;
  5. const int MAXN=1e5+4;
  6. ll a[MAXN];//分钟
  7. ll p[MAXN];//价格
  8. ll x[MAXN];//库存
  9. int main() {
  10. int n;
  11. cin>>n;
  12. for (int i=1; i<=n; i++) {
  13. cin>>a[i]>>p[i]>>x[i];
  14. }
  15. ll ans=1e9+4;
  16. bool flag=false;
  17. for (int i=1; i<=n; i++) {
  18. if (x[i]>a[i]) {
  19. ans=min(ans, p[i]);
  20. flag=true;
  21. }
  22. }
  23. if (true==flag) {
  24. cout<<ans<<"\n";
  25. } else {
  26. cout<<"-1\n";
  27. }
  28. return 0;
  29. }

C - Unexpressed

https://atcoder.jp/contests/abc193/tasks/abc193_c

给一个 N,求区间 [1, N] 中不能表示为 ab 数的个数。a 和 b 不小于数字 2。

如果本题不看 N 的范围,那么我们可以直接穷举每个数据 i,测试 i 能否表示为 ab。框架代码如下:

  1. for (int i=1; i<=n; i++) {
  2. for (int a=2; a*a<=i; a++) {
  3. for (int b=2; b<=a; b++) {
  4. }
  5. }
  6. }

这样代码的时间复杂度为 O(NlogNlogN)。但是考虑本题 N 为 1010,因此这样肯定是 TLE。

我们可以借鉴筛法来解决这个问题。

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. typedef long long ll;
  5. int main() {
  6. ll n;
  7. cin>>n;
  8. vector<bool> flag(1e5+4, false);
  9. ll ans=n;
  10. for (ll i=2; i*i<=n; i++) {
  11. if (flag[i]) {
  12. //已经标志过
  13. continue;
  14. }
  15. ll t=i*i;
  16. while (t<=n) {
  17. ans--;
  18. if (t<flag.size()) {
  19. flag[t]=true;
  20. }
  21. t*=i;
  22. }
  23. }
  24. cout<<ans<<"\n";
  25. return 0;
  26. }

D - Poker

https://atcoder.jp/contests/abc193/tasks/abc193_d

看了一下题目,好像是组合数学问题,苍天啊。题目好长,英文看得脑壳疼。

根据题目的描述,我们可以推导出如下公式:

{CxCyxyCx(Cx1)x=y,其中 Ci 表示的是没有翻开的牌 i。

  1. #include <iostream>
  2. #include <vector>
  3. #include <numeric>
  4. #include <iomanip>
  5. using namespace std;
  6. typedef long long ll;
  7. ll check(string s) {
  8. vector<ll> cnt(10);
  9. ll res=0;
  10. for (int i=0; i<5; i++) {
  11. cnt[s[i]-'0']++;
  12. }
  13. for (int i=1; i<=9; i++) {
  14. ll t=i;
  15. while (cnt[i]--) {
  16. t*=10;
  17. }
  18. res+=t;
  19. }
  20. return res;
  21. }
  22. int main() {
  23. int k;
  24. string s, t;
  25. cin>>k>>s>>t;
  26. vector<ll> card(10, k);
  27. for (int i=0; i<4; i++) {
  28. card[s[i]-'0']--;
  29. card[t[i]-'0']--;
  30. }
  31. ll res = 9*k-8;
  32. ll tot = res*(res-1);
  33. ll ans=0;
  34. for (int i=1; i<=9; i++) {
  35. for (int j=1; j<=9; j++) {
  36. s[4] = '0'+i;
  37. t[4] = '0'+j;
  38. if (check(s)<=check(t)) {
  39. continue;
  40. }
  41. if (i==j) {
  42. ans += card[i]*(card[i]-1);
  43. } else {
  44. ans += card[i]*card[j];
  45. }
  46. }
  47. }
  48. cout<<fixed<<setprecision(18)<<1.0*ans/tot<<"\n";
  49. return 0;
  50. }

E - Oversleeping

https://atcoder.jp/contests/abc193/tasks/abc193_e

题目推敲了半天,尼玛,就是中国剩余定理(Chinese Remainder Theorem, CRT)啊。直接利用 AtCoder 提供的 Math 包里的 crt 算法就可以了。

  1. #include <bits/stdc++.h>
  2. #include <atcoder/math>
  3. using namespace std;
  4. typedef long long ll;
  5. int main() {
  6. int t;
  7. cin>>t;
  8. while (t--) {
  9. ll x,y, p, q;
  10. cin>>x>>y>>p>>q;
  11. ll ans=LLONG_MAX;
  12. for (ll i=x; i<x+y; i++) {
  13. for (ll j=p; j<p+q; j++) {
  14. auto [t, lcm] = atcoder::crt({i, j}, {(x+y)*2, p+q});
  15. if (0==lcm) {
  16. continue;
  17. }
  18. ans = min(ans, t);
  19. }
  20. }
  21. if (ans==LLONG_MAX) {
  22. cout<<"infinity\n";
  23. } else {
  24. cout<<ans<<"\n";
  25. }
  26. }
  27. return 0;
  28. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/52652
推荐阅读
相关标签
  

闽ICP备14008679号