LeetCode·每日一题·2681. 英雄的力量·脑筋急转弯

题目

示例

思路

代码


static const int MOD = 1e9 + 7;
int cmp(const void *a, const void *b) 
{
    return *(int *)a - *(int *)b;
}
int sumOfPower(int* nums, int numsSize){
    qsort(nums, numsSize, sizeof(nums[0]), cmp);//排序
    long long ans = 0, s = 0;
    for (int x = 0; x < numsSize; ++x) {// x 作为最大值
        ans = (ans + (((long long)nums[x] * (long long)nums[x])) % MOD * (nums[x] + s)) % MOD; // 中间模一次防止溢出
        s = (s * 2 + nums[x]) % MOD; // 递推计算下一个 s
    }
    return ans;
}

作者:小迅
链接:https://leetcode.cn/problems/power-of-heroes/solutions/2367314/nao-jin-ji-zhuan-wan-zhu-shi-chao-ji-xia-ovei/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/m0_64560763/article/details/132036704