[Solution] [国家集训队]整数的lqp拆分
《组合数学》很重要的嘞
真诚地建议大家去学习一下《组合数学(第五版)(卢开诚,卢华明著)》
可能很多人觉得没用,但我觉得你去学一下学个一知半解也不错
那么回到本题:
题意:很简单,对数进行拆分乘上对应的斐波那契数列,求和
分析:
首先了解到\(Fibonacci\)的数列的母函数为:
\(F(x)=\frac{x}{1-x-x^2}\)
记答案的母函数为:\(A(x)\)
通过分析题目我们可以得到:
\[A(x)=\sum\limits_{i=0}^{n}F(x)^{i}
\]
由普通型母函数:\(G(x)=\sum\limits_{i=0}^{\infty}x^i=\frac{1}{1-x}\)(多项式竖式很容易证)可得:
\[A(x)=\frac{1}{1-F(x)}=\frac{1-x-x^2}{1-2x-x^2}
\]
再将母函数进行彻底化简,求出\(x^n\)的系数,即为答案
读者可以自己下去推导,这里就不证明了
\(ans=a_{n-1}=a\times(1-\sqrt{2})^{n-1}+b\times(1+\sqrt{2})^{n-1}\)
\(\left\{ \begin{array}{ll} a &=\frac{\sqrt{2}-1}{2\sqrt{2}}\\ b&=\frac{\sqrt{2}+1}{2\sqrt{2}} \end{array} \right.\)
这个时候因为有无理数取模,需要用到二次剩余定理,不会的小朋友们出门左拐:二次剩余
先给结论:\(\sqrt{2}\)在模\(P=1e9+7\)的条件下为\(59713600\)
考虑\(n\)过大,根据费马小定理得\(a^{p-1}\equiv1(mod\quad P)\)
所以读的时候可以边读入边模\(P-1\),然后加个快速幂模板,你就可以成功\(AC\)了
AC Code
#include<bits/stdc++.h>
#define re register
#define int long long
#define FOR(i,a,b) for(re int i=(a),i##i=(b);i<=i##i;i++)
using namespace std;
const int P=1e9+7;
int fast_power(int base,int pow);
signed main() {
int x=485071604;
int y=940286408;
int a=514928404;
int b=59713601;
int n=0;
char ch='0';
while(ch<='9'&&ch>='0') {
ch=getchar();
if(ch>'9'||ch<'0') break;
n=(n*10+(ch-'0'))%(P-1);
}
cout<<(x%P*fast_power(y,n-1)+a%P*fast_power(b,n-1)%P)%P;
return 0;
}
int fast_power(int base,int pow) {
int sum=1;
while(pow) {
if(pow&1) sum=sum%P*base%P;
pow>>=1,base=base%P*base%P;
}
return sum;
}