The 2024 ICPC in Hubei Province, China

文章介绍了几个C++编程题目,涉及gcd、最大公约数、最小公倍数的计算,以及使用海伦公式求解三角形面积的算法,还涉及到模运算和逆元的应用。
摘要由CSDN通过智能技术生成

在这里插入图片描述

A. Long Live

易发现:a = 1 时达到最大值

code:

#include<bits/stdc++.h>
#define int long long

using namespace std;

void solve(){
    int x,y; cin >> x >> y;
    int gd = __gcd(x,y);
    int cnt = x * y / gd / gd;
    int mx = -1;
    int p1 = 1,p2 = cnt;
    cout << p1 << " " << p2 << endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int t; cin >> t;
    while(t --){
        solve();
    }
    return 0;
}

B. Nana Likes Polygons

首先三角形都为凸多边形,此题实际求的就是最小的组成的三角形的面积,数据很小,所以可以暴力枚举然后通过:海伦公式 求出答案
在这里插入图片描述

code:

#include<bits/stdc++.h>

using namespace std;

void solve() {
    int n; cin >> n;
    int a[n], b[n];
    for (int i = 0; i < n; i ++) {
        cin >> a[i] >> b[i];
    }
    double ans = -1;
    for (int i = 0; i < n; i ++) {
        for (int j = i + 1; j < n; j ++) {
            for (int k = j + 1; k < n; k ++) {
                double x1 = a[i], y1 = b[i], x2 = a[j], y2 = b[j], x3 = a[k], y3 = b[k];
                double k1 = 1.0 * (y2 - y1) / (x2 - x1), k2 = 1.0 * (y3 - y1) / (x3 - x1);
                if (k1 == k2) continue;

                double x = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
                double y = sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1));
                double z = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));

                double p = 1.0 * (x + y + z) / 2.0;
                double cnt = sqrt(p * (p - x) * (p - y) * (p - z));
                if (ans == -1) ans = cnt;
                else ans = min(ans, cnt);
          //      cout << cnt << endl;
            }
        }
    }
    if (ans == -1) {
        cout << "-1" << endl;
        return;
    }
    printf("%.6lf\n", ans);
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int t; cin >> t;
    while(t --){
        solve();
    }
    return 0;
}

E. Spicy or Grilled?

大铁签题

code:

#include<bits/stdc++.h>
#define int long long

using namespace std;

signed main(){
    int t; cin >> t;
    while(t --){
        int x,y; cin >> x >> y;
        int a,b; cin >> a >> b;
        int ans = y * b + (x - y) * a;
        cout << ans << endl;
    }
}

J. Points on the Number Axis A

我是将通过 x i + x j 2 \dfrac{x_i+x_j}{2} 2xi+xj 式子的形式将所有的可能得到的最后的点加起来
最后可以得到 x 1 {x_1} x1 + x 2 {x_2} x2 + … + x n {x_n} xn的结果
然后可能得到的点有 n 种 所以答案也就是 x 1 + x 2 + . . . + x n n \dfrac{x_1+x_2 + ... + x_n}{n} nx1+x2+...+xn
( x 1 + x 2 + . . . + x n ) ⋅ n − 1   m o d   998   244   353 ({x_1+x_2 + ... + x_n})\cdot n^{-1} \bmod 998\,244\,353 (x1+x2+...+xn)n1mod998244353.
所以也需要用到逆元

code:

#include<bits/stdc++.h>
#define int long long

using namespace std;
const int mod = 998244353;

int qsm(int a,int b){
    int ans = 1;
    while(b){
        if(b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    } return ans;
}

const int N = 1e6 + 7;
int n;
int a[N];

void solve(){
    cin >> n;
    int sum = 0;
    for(int i = 0;i < n;i ++){
        cin >> a[i];
        sum = (sum + a[i]) % mod;
    }
    int fk = qsm(n,mod - 2);
    int ans = sum * fk % mod;
    cout << ans << endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int t; t = 1;
    while(t --){
        solve();
    }
    return 0;
}

L. LCMs

注意范围是 ( 2 ≤ a ≤ b ≤ 1 0 7 2 \le a \le b \le 10^7 2ab107)
然后需要特判一下 if(a == b) ans = 0;
还有种情况就是 gcd(a,b) != 1 时 其成本 ans = min(ans,a + b) , 只看最小质因子分析不一定最优,因为二者有相同的质因子,并不互质
其次 a -> b成本有以下几种可能,前提在 a 和 b 都存在最小质因子 x 和 y 情况下分析。此时 a 到 x 成本即 a ,b 到 y 的成本即 b 。 其余情况可见代码:

  • lcm(a,b)
  • lcm(a,2) + lcm(2,b)
  • a + lcm(x,b)
  • lcm(a,y) + b
  • a + lcm(x,y) + b
  • a + lcm(x,2) + lcm(2,y) + b

code:

#include<bits/stdc++.h>
#define int long long

using namespace std;

int a,b;

int lcm(int x,int y){
    if(x == y) return 0;
    return x * y / __gcd(x,y);
}

void solve(){
    cin >> a >> b;
    int x = -1,y = -1;
    for(int i = 2;i <= a / i;i ++){
        if(a % i) continue;
        x = i;
        break;
    }
    for(int i = 2;i <= b / i;i ++){
        if(b % i) continue;
        y = i;
        break;
    }
    int ans = min(lcm(a,2) + lcm(2,b),lcm(a,b)); 
    if(__gcd(a,b) != 1) ans = min(ans,a + b);
    if(x != -1 && y != -1){
        ans = min(ans,a + lcm(x,b));
        ans = min(ans,lcm(a,y) + b);
        ans = min(ans,a + lcm(x,y) + b);
        ans = min(ans,a + lcm(x,2) + lcm(2,y) + b);
    } else if(x == -1 && y != -1){
        ans = min(ans,lcm(a,2) + lcm(2,y) + b);
        ans = min(ans,lcm(a,y) + b);
    } else if(x != -1 && y == -1){
        ans = min(ans,a + lcm(x,2) + lcm(2,b));
        ans = min(ans,a + lcm(x,b)); 
    }
    cout << ans << endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int t; cin >> t;
    while(t --){
        solve();
    }
    return 0;
}
  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值