洛谷5104 - 红包发红包

在 $[0,w]$ 中等概率取出的值的期望是 $\frac{1}{2} w$ ,简单观察可以发现,答案就是 $\frac{1}{2} ^ k$ ,快速幂一下就好了。

需要注意的是,根据欧拉定理,若 $p$ 是质数:
$$a^b \equiv a ^ {b \mod \varphi(p)} (\mod p)$$
所以这题如果你直接给 $k$ 模下 $p$ 就会挂啦 233.

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// =================================
// author: memset0
// date: 2018.12.16 14:30:19
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
namespace ringo {
typedef long long ll;

template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void maxd(T &a, T b) { if (b > a) a = b; }
template <class T> inline void mind(T &a, T b) { if (b < a) a = b; }
template <class T> inline void print(T x, char c) { print(x), putchar(c); }
template <class T> inline T abs(const T &a) { if (a < 0) return -a; return a; }

const int p = 1e9 + 7;
int w;
ll n, k;

int fpow(int a, int b) {
int s = 1;
while (b) {
if (b & 1) s = (ll)s * a % p;
b >>= 1, a = (ll)a * a % p;
}
return s;
}

void main() {
read(w), read(n), read(k);
print((ll)w * fpow(5e8 + 4, k % (p - 1)) % p, '\n');
}

} signed main() { return ringo::main(), 0; }
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×