D.Divide and Sum

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
对a的元素进行排序。
将a的元素放到p,q两个队列中,对每一个元素对f的贡献值单独考虑。
对于f(xi,yi)=|xi-yi|,当中大的数做正贡献,小的做负贡献。
对于ai个元素假设前面有k个元素在p队列,则i-1-k个元素在q
如果要ai有正贡献,则k>=n-(i-1-k);否则负贡献。
如果在q队列中对于ai,设有k个在q队列中,则i-1-k个在q队列中,要做正贡献,则k>=n-(i-1-k)‘
两个的结果的是i>n做正贡献,否则负贡献。
所以对于所有的排列组合中,i>n的元素都是正贡献,小于等于n的都是负贡献。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
int a[1000001];
long long int qp(long long int a,long long int b,long long int mod)
{
    long long ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=ans*a;
            ans%=mod;
        }
        a=a*a;
        a%=mod;
        b>>=1;
    }
    return ans;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
#endif
    int i, j, n, k;
    long long int v, ans = 0, mod = 998244353, u;
    scanf("%d", &n);
    for (i = 0; i < 2 * n; i++)
    {
        scanf("%d", &a[i]);
    }
    sort(a, a + 2 * n);
    u = 1;
    v = 1;
    for (i = 1; i <= n; i++)
    {
        u = (u * (n + i)) % mod;
        v = (v * i) % mod;
    }
    v=qp(v,mod-2,mod);
    u = (u*v) % mod;
    for (i = 0; i < n; i++)
    {
        ans += a[i + n] - a[i];
        ans %= mod;
    }
    printf("%lld\n", (ans*u) % mod);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现上述功能的Java程序: ```java public class RealNumber { private double num; public RealNumber(double num) { this.num = num; } public RealNumber add(RealNumber other) { return new RealNumber(this.num + other.num); } public RealNumber subtract(RealNumber other) { return new RealNumber(this.num - other.num); } public RealNumber multiply(RealNumber other) { return new RealNumber(this.num * other.num); } public RealNumber divide(RealNumber other) { if (other.num == 0) { throw new IllegalArgumentException("Divisor cannot be zero"); } return new RealNumber(this.num / other.num); } public RealNumber opposite() { return new RealNumber(-this.num); } public RealNumber reciprocal() { if (this.num == 0) { throw new IllegalArgumentException("Cannot take reciprocal of zero"); } return new RealNumber(1 / this.num); } public RealNumber absolute() { return new RealNumber(Math.abs(this.num)); } public RealNumber max(RealNumber other) { return new RealNumber(Math.max(this.num, other.num)); } public RealNumber min(RealNumber other) { return new RealNumber(Math.min(this.num, other.num)); } public RealNumber ceil() { return new RealNumber(Math.ceil(this.num)); } public RealNumber floor() { return new RealNumber(Math.floor(this.num)); } public RealNumber round() { return new RealNumber(Math.round(this.num)); } public RealNumber sqrt() { if (this.num < 0) { throw new IllegalArgumentException("Cannot take square root of negative number"); } return new RealNumber(Math.sqrt(this.num)); } public RealNumber log() { if (this.num <= 0) { throw new IllegalArgumentException("Cannot take logarithm of non-positive number"); } return new RealNumber(Math.log(this.num)); } public RealNumber pow(RealNumber other) { return new RealNumber(Math.pow(this.num, other.num)); } } ``` 这个程序定义了一个 `RealNumber` 类,封装了一个浮点数。其中的各种方法实现了题目中要求的各种操作。 以下是一个示例程序,演示如何使用 `RealNumber` 类: ```java public class RealNumberDemo { public static void main(String[] args) { RealNumber a = new RealNumber(3.5); RealNumber b = new RealNumber(2.0); RealNumber sum = a.add(b); System.out.println("Sum: " + sum); RealNumber difference = a.subtract(b); System.out.println("Difference: " + difference); RealNumber product = a.multiply(b); System.out.println("Product: " + product); RealNumber quotient = a.divide(b); System.out.println("Quotient: " + quotient); RealNumber opposite = a.opposite(); System.out.println("Opposite of " + a + ": " + opposite); RealNumber reciprocal = a.reciprocal(); System.out.println("Reciprocal of " + a + ": " + reciprocal); RealNumber absolute = a.absolute(); System.out.println("Absolute value of " + a + ": " + absolute); RealNumber max = a.max(b); System.out.println("Max of " + a + " and " + b + ": " + max); RealNumber min = a.min(b); System.out.println("Min of " + a + " and " + b + ": " + min); RealNumber ceil = a.ceil(); System.out.println("Ceiling of " + a + ": " + ceil); RealNumber floor = a.floor(); System.out.println("Floor of " + a + ": " + floor); RealNumber round = a.round(); System.out.println("Rounded value of " + a + ": " + round); RealNumber sqrt = a.sqrt(); System.out.println("Square root of " + a + ": " + sqrt); RealNumber log = a.log(); System.out.println("Natural logarithm of " + a + ": " + log); RealNumber power = a.pow(b); System.out.println(a + " raised to the power of " + b + ": " + power); } } ``` 输出结果: ``` Sum: 5.5 Difference: 1.5 Product: 7.0 Quotient: 1.75 Opposite of 3.5: -3.5 Reciprocal of 3.5: 0.2857142857142857 Absolute value of 3.5: 3.5 Max of 3.5 and 2.0: 3.5 Min of 3.5 and 2.0: 2.0 Ceiling of 3.5: 4.0 Floor of 3.5: 3.0 Rounded value of 3.5: 4.0 Square root of 3.5: 1.8708286933869707 Natural logarithm of 3.5: 1.252762968495368 3.5 raised to the power of 2.0: 12.25 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值