为什么,我,不会,数学!
赛时感想
有猪头看错题写成 第 N 层了,想着还挺简单一个高精板子 + Lucas 就搞定了。
然后就寄了。等发现的时候黄花菜都凉了,一时半会也没推出来,最后含恨而终。
言归正传,还是来看看数学推导吧!
数学推导
前排提醒:本题还需要前置知识 Lucas 定理,如果已经完全掌握 Lucas 定理可以先跳过这一部分直接看后面数学推导。
Lucas 定理在本题的应用推导
Lucas 定理原理推导传送门(oi-wiki):Lucas 定理
我们这里只需要使用 Lucas 定理的一个最基本的定理:
对于质数 ,组合数 模 等于各位分别组合数的乘积再模 。
写得通俗一点,就是:
如果将 和 分别写成 进制:
则:
非常好!我们只需要这个结论就可以做这个题。
我们想知道,第 层有多少个偶数,其实等价于第 层数的总数减去第 层有多少个奇数。而第 层有几个数呢? 个。所以问题变成我们要求第 层有多少个奇数。
考量这个层数 ,我们会惊讶的发现,这一层的所有数就是 的所有数,而它们的奇偶性可以通过对 取模得到。
至此,我们把题目写成 Lucas 定理的形式:
只有当右侧所有 ,才会有 (连乘全为奇结果才为奇)。
此时, 与 均只能取到 或 (二进制只有 )。
排列组合一下:
- , :
- , :
- , :
- , :
因此,只要 ,则 ,而 不受限制。
也就是说,对于数 的二进制数,如果它第 位是 ,即 ,那么这个数位一定会贡献 的第 位 的两种选择 和 ,使得 。
而如果 总共有 个二进制数位,其中 个二进制数位是 ,那么对应的 的所有可能情况数就是 (其实是 , 代表这个数位被固定, 代表这个数位有两种取值)。
终于!我们得到了,第 层有 个奇数,其中 是 二进制数位 的个数。
也由此,第 层有 个偶数,推导完毕。
回归本题
我们前面花了不少篇幅去推导第 层的奇数个数,那么该如何使用这个公式呢?
首先,我们回看一下这个杨辉三角,注意第 层有 个数。
1 #第0层
1 1
1 2 1
1 3 3 1
...那么比较显然的,第零层和第一层都是 个偶数了,我们记前 层的偶数总个数为 ,那么 ,。
我们另记: 表示前 层有多少个奇数。
(注意,这里前 层是包括了第 层不包括第 层,所以假设我们要求第 层及之前的所有层数,应该写作 )。
容易得到:
则:
其中 是前 层数的总数。
接下来,我们需要敏锐的想到,这个题大概率需要根据奇偶进行分类。
所以,我们分别考虑 和 。
对于 ,我们考虑它与 的关系。
因为 是由 在二进制位上左移一位得到,相当于在右边加了一个 ,那么很显然 。而 相当于在右边加上一个 ,那么也有 。
所以:
对于 ,比 多了一个 这一层,那么:
而 恰好就是 ,所以:
推完了 的递推式,该回代回原 了,于是最终得到:
多项式计算不再展开了,注意代入的时候是 就可以了。
这便是整个递推式的完整推导过程,终于写完了 TAT。
代码
回到代码上,其实就是朴实的高精 + 记忆化递归。
高精代码比较长,非常建议留一个板子以后万一用到了。
记忆化递归部分内容代码不长,简单参考一下吧。
#include <bits/stdc++.h>
using namespace std;
class BigInt // 新定义大整数类BigInt,可以进行高精度运算,并且重载了部分运算符可以方便计算
{
public:
vector<long long> N;
private:
void trim()
{
while (N.size() > 1 && N.back() == 0)
N.pop_back();
if (N.empty())
N.push_back(0);
}
public:
BigInt(){
N.push_back(0);
}
BigInt(long long n)
{
if (n == 0)
{
N.push_back(0);
return;
}
while (n > 0)
{
N.push_back(n % 10);
n /= 10;
}
}
BigInt operator*(int num);
BigInt operator*(const BigInt &num) const;
BigInt operator/(int num);
BigInt operator-(const BigInt &num) const;
BigInt operator+(const BigInt &num) const;
bool operator==(const BigInt &num) const;
bool operator!=(const BigInt &num) const;
bool operator>(const BigInt &num) const;
bool operator<(const BigInt &num) const;
friend ostream& operator<<(ostream &out, const BigInt &num);
friend istream& operator>>(istream &in, BigInt &num);
friend BigInt calc(BigInt N);
};
BigInt BigInt::operator*(int num)
{
BigInt res;
res.N.clear();
long long carry = 0;
for (int i = 0; i < N.size() || carry; i++)
{
long long current = carry;
if (i < N.size())
current += N[i] * num;
res.N.push_back(current % 10);
carry = current / 10;
}
res.trim();
return res;
}
BigInt BigInt::operator*(const BigInt &num) const
{
BigInt result;
result.N.assign(N.size() + num.N.size(), 0);
for (size_t i = 0; i < N.size(); ++i)
{
for (size_t j = 0; j < num.N.size(); ++j)
{
result.N[i + j] += N[i] * num.N[j];
}
}
long long carry = 0;
for (size_t i = 0; i < result.N.size(); ++i)
{
long long sum = result.N[i] + carry;
result.N[i] = sum % 10;
carry = sum / 10;
}
while (carry > 0)
{
result.N.push_back(carry % 10);
carry /= 10;
}
result.trim();
return result;
}
BigInt BigInt::operator/(int num) {
BigInt res;
res.N.clear();
long long remainder = 0;
for (int i = N.size() - 1; i >= 0; --i)
{
remainder = remainder * 10 + N[i];
int digit = remainder / num;
res.N.push_back(digit);
remainder = remainder % num;
}
reverse(res.N.begin(), res.N.end());
res.trim();
return res;
}
BigInt BigInt::operator+(const BigInt &num) const
{
BigInt res;
res.N.clear();
long long carry = 0;
int maxlen = max(N.size(), num.N.size());
for (int i = 0; i < maxlen || carry; ++i)
{
long long sum = carry;
if (i < N.size()) sum += N[i];
if (i < num.N.size()) sum += num.N[i];
res.N.push_back(sum % 10);
carry = sum / 10;
}
res.trim();
return res;
}
BigInt BigInt::operator-(const BigInt &num) const
{
BigInt res;
res.N.clear();
long long borrow = 0;
for (int i = 0; i < N.size(); ++i)
{
long long sub = N[i] - borrow;
if (i < num.N.size())
sub -= num.N[i];
if (sub < 0)
{
sub += 10;
borrow = 1;
}
else
{
borrow = 0;
}
res.N.push_back(sub);
}
res.trim();
return res;
}
bool BigInt::operator==(const BigInt &num) const
{
if (N.size() != num.N.size())
return false;
for (size_t i = 0; i < N.size(); ++i)
if (N[i] != num.N[i])
return false;
return true;
}
bool BigInt::operator!=(const BigInt &num) const
{
return !(*this == num);
}
bool BigInt::operator>(const BigInt &num) const {
if (N.size() != num.N.size())
return N.size() > num.N.size();
for (int i = N.size() - 1; i >= 0; --i)
if (N[i] != num.N[i])
return N[i] > num.N[i];
return false;
}
bool BigInt::operator<(const BigInt &num) const {
if (N.size() != num.N.size())
return N.size() < num.N.size();
for (int i = N.size() - 1; i >= 0; --i)
if (N[i] != num.N[i])
return N[i] < num.N[i];
return false;
}
ostream& operator<<(ostream &out, const BigInt &num) {
for (int i = num.N.size() - 1; i >= 0; i--)
out << num.N[i];
return out;
}
istream& operator>>(istream &in, BigInt &num)
{
num.N.clear();
string s;
in >> s;
if (!in) return in;
for (int i = s.size() - 1; i >= 0; --i)
num.N.push_back(s[i] - '0');
num.trim();
return in;
}
// 以上全部为对于大整数BigInt的定义和运算符重载,属于模板(总不能指望考场全部现写吧!)
// 这部分具体不在这里展开了,考场上也只能直接对着板子抄
map<BigInt, BigInt> memo; // 对已计算过的值进行记忆化存储
// 因为大数不可能再用数组下标存储了,这里只能使用map进行映射
BigInt calc(BigInt N)
{
if (memo.count(N)) return memo[N]; // 如果存储过,直接返回对应值
if(N == 1) return 0;
if(N == 2) return 0;
// 注意,这里需要解读一下,原题义给出的第二行是1 2 1,我们这里将杨辉三角补齐
// 那么第一行是 1,第二行是 1 1,所以1和2返回0
BigInt n = N/2,res; // 简单存一下N/2,后面公式方便解读一点
if(N - n * 2 == 1) // 奇数 因为取模很难写所以简化一下,单独考虑模2可以写成先除2再乘2作差取余数
res = calc(n)*2 + calc(n+1) + n*(n+1)/2;
else // 偶数
res = calc(n)*3 + n*(n-1)/2;
// 计算结果,顺便存一下值,后续记忆化可以复用值
memo[N] = res;
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
BigInt N;
while(cin >> N)
cout << calc(N+1) << endl; // 因为补齐了杨辉三角第0行,所以行数要+1
return 0;
}二编:写了很久求过啊QAQ
三编:猪头博主忘黏代码了,有点无语