【贪心】【快速幂】牛客模拟赛#2 C-涨薪

it2025-04-16  4

小目录

链接题目描述样例输入 #1样例输出 #1样例输入 #2样例输出 #2数据范围或提示思路代码

链接

题目描述

样例输入 #1

2 1 1 1 5 3

样例输出 #1

21

样例输入 #2

2 2 0 0 5 2

样例输出 #2

0

数据范围或提示

思路

显然我们选择最大的 X X X个翻三倍,其余的 Y Y Y个翻两倍,剩下的评C 那么我们求快速幂就好了 只不过如果m = 1的情况,要把C的也加进去

代码

#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #define ll long long using namespace std; const int tjh = 1e9 + 7;//%大佬 int n, m, X, Y, suma, sumb; int a[100005]; bool cmp(int x, int y) {return x > y;} int ksm(int x, int p) { int ans = 1; x %= tjh; while(p) { if (p & 1) ans = (ll)ans * x % tjh; x = (ll)x * x % tjh; p >>= 1; } return ans % tjh; }//快速幂 int main() { scanf("%d%d%d%d", &n, &m, &X, &Y); int w1 = ksm(3, m); int w2 = ksm(2, m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); sort(a + 1, a + n + 1, cmp); for (int i = 1; i <= X; ++i) suma = (suma + a[i]) % tjh; for (int j = X + 1; j <= X + Y; ++j) sumb = (sumb + a[j]) % tjh; int ansx = (((ll)suma * w1) % tjh + ((ll)sumb * w2) % tjh) % tjh; if (m == 1) for (int i = X + Y + 1; i <= n; ++i) ansx = (ansx + a[i]) % tjh; printf("%d", ansx); return 0; }
最新回复(0)