题解:P15889 [COCI 2025/2026 #6] 零花钱 / Džeparac

· · 题解

题目传送门

思路

设母亲留下的金额为 k,则分给儿子的总金额为 m = N - k。由于每天两个儿子得到相同金额,所以每天总支出 x 是偶数,故 m 必须为偶数。

将 m 拆分为若干天每天得到的金额(正整数),顺序重要,这就是组合数学中的整数分拆问题。当 m \ge 1 时,m 的正整数有序拆分个数为 2^{m-1};当 m = 0 时,对应 k = N,只有一种方案(不分配)。

因此,对于每个可能的 m(0 \le m \le \lfloor N/2 \rfloor),若 m = 0 贡献 1,否则贡献 2^{m-1}。总方案数为:

\sum_{m=1}^{\left \lfloor n{\div} 2 \right \rfloor } 2^{m-1}+1=2^{\left \lfloor n{\div} 2 \right \rfloor }

同时注意到 n\le 10^6,所以使用快速幂简单优化再计算即可,时间复杂度为 O(\log_n)。

code

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
//十年OI一场空,不开long long见祖宗
LL n;
const int P = 1000000007;
LL p(LL a , LL b){//注意到 pow 是保留字(除了我应该没有人会忘吧qwq)
    LL res = 1;
    while(b){
        if(b & 1)
            res = res * a % P;
        a = a * a % P;
        b >>= 1;
    }
    return res;
}
int main(){
    scanf("%lld" , &n);
    printf("%lld" , p(2 , n / 2));
    return 0;
}