这题找规律。
这道题 G ( B ) G(B) G(B) 的定义如下:
- 把集合 B B B 中的元素从大到小排序,得到 B = { b 1 , b 2 … , b c n t } ( c n t B=\{b_1,b_2\dots,b_{cnt}\}(cnt B={b1,b2…,bcnt}(cnt 为集合元素个数 ) ) )。则 G ( B ) = ∑ i = 1 c n t ( ( − 1 ) i + 1 × b i ) G(B)=\sum\limits_{i=1}^{cnt}\Big((-1)^{i+1}\times b_i\Big) G(B)=i=1∑cnt((−1)i+1×bi)。
其中集合 B B B 是集合 A A A 的子集,集合 A A A 为 { 1 , 2 , 3 , … , n } \{1,2,3,\dots,n\} {1,2,3,…,n},如果不知道什么叫子集的话,可以看这里。
我们先列举 n n n 较小的情况:
1.当 n = 1 n = 1 n=1 时:
a n s = 1 ans = 1 ans=1
2.当 n = 2 n = 2 n=2 时:
a n s = 1 + 2 + ( 2 − 1 ) = 4 ans = 1 + 2 + (2 - 1) = 4 ans=1+2+(2−1)=4
3.当 n = 3 n = 3 n=3 时:
a n s = 1 + 2 + 3 + ( 2 − 1 ) + ( 3 − 1 ) + ( 3 − 2 ) + ( 3 − 2 + 1 ) = 12 ans = 1 + 2 + 3 + (2 - 1) + (3 - 1) + (3 - 2) + (3 - 2 + 1) = 12 ans=1+2+3+(2−1)+(3−1)+(3−2)+(3−2+1)=12
4.当 n = 4 n = 4 n=4 时:
a n s = 1 + 2 + 3 + 4 + ( 2 − 1 ) + ( 3 − 1 ) + ( 4 − 1 ) + ( 3 − 2 ) + ( 4 − 2 ) + ( 4 − 3 ) + ( 3 − 2 + 1 ) + ( 4 − 2 + 1 ) + ( 4 − 3 + 1 ) + ( 4 − 3 + 2 ) + ( 4 − 3 + 2 − 1 ) = 32 ans = 1 + 2 + 3 + 4 + (2 - 1) + (3 - 1) + (4 - 1) + (3 - 2) + (4 - 2) + (4 - 3) + (3 - 2 + 1) + (4 - 2 + 1) + (4 - 3 + 1) + (4 - 3 + 2) + (4 - 3 + 2 - 1) = 32 ans=1+2+3+4+(2−1)+(3−1)+(4−1)+(3−2)+(4−2)+(4−3)+(3−2+1)+(4−2+1)+(4−3+1)+(4−3+2)+(4−3+2−1)=32
将 a n s ans ans 分别除以 n n n,结果为 1 1 1、 2 2 2、 4 4 4、 8 8 8,可以发现,它们都可以写成 2 x 2^x 2x 的形式,即: 2 0 2^0 20、 2 1 2^1 21、 2 2 2^2 22、 2 3 2^3 23,即: 2 n − 1 2^{n - 1} 2n−1。那么 a n s ans ans 即为 n × 2 n − 1 n \times 2^{n - 1} n×2n−1,快速幂算出结果即可。
Code:
#include <bits/stdc++.h>
#define int long long
#define mod 911451407
using namespace std;
int n, ans, a = 2;
main() {
cin >> n;
ans = n % mod;
n -= 1;
while (n) {
if (n & 1) ans = ans * a % mod;
a = a * a % mod;
n >>= 1;
}
cout << ans;
}