[Solution] [国家集训队]整数的lqp拆分

《组合数学》很重要的嘞

题意：很简单，对数进行拆分乘上对应的斐波那契数列，求和

$F(x)=\frac{x}{1-x-x^2}$

$A(x)=\sum\limits_{i=0}^{n}F(x)^{i}$

$A(x)=\frac{1}{1-F(x)}=\frac{1-x-x^2}{1-2x-x^2}$

$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.$

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;
}

posted @ 2021-03-09 14:32  雪落た兮赏翩舞  阅读(50)  评论(0编辑  收藏  举报