51Nod1305 Pairwise Sum and Divide

51Nod1305 Pairwise Sum and Divide

题意

有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:

fun(A)
sum = 0
for i = 1 to A.length
for j = i+1 to A.length
sum = sum + Floor((A[i]+A[j])/(A[i]*A[j]))
return sum

给出数组A,由你来计算fun(A)的结果。例如:A = {1, 4, 1},fun(A) = [5/4] + [2/1] + [5/4] = 1 + 2 + 1 = 4。

题解

(a + b) / (a * b) 等价于 1 / b + 1 / a.

a == 1a == 2a 为其他值
b == 1211
b == 2110
b 为其他值100

所以答案为 C(2, cnt1) * 2 + C(cnt2 * 2) + C(cnt1, others + cnt2)

AC代码

#include <cstdio>
#include <regex>
using namespace std;
const int ARRSIZE = 100100;
long long arr[ARRSIZE];
int main() {
    int nNum; scanf("%I64d", &nNum);
    long long cnt1 = 0, cnt2 = 0, others = 0;
    for(int i = 0; i < nNum; i++) {
        long long num; scanf("%I64d", &num);
        if(num == 1) cnt1++;
        else if(num == 2) cnt2++;
        else others++;
    }
    printf("%I64d", cnt1 * (cnt1 - 1) + cnt1 * (others + cnt2) + cnt2 * (cnt2 - 1) / 2);
    return 0;
}

转载于:https://www.cnblogs.com/1pha/p/8877428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值