51nod1149 Pi的递推式 组合数学

版权声明:转吧转吧这条东西只是来搞笑的。。 https://blog.csdn.net/jpwang8/article/details/88126157

Description


定义 f ( x ) = { 1 ,    x [ 0 , 4 ) f ( x 1 ) + f ( x π ) ,    x [ 0 , + ) f(x)=\left\{ \begin{aligned} 1,\;&x\in[0,4)\\ f(x-1)+f(x-\pi),\;&x\in[0,+\infty)\\ \end{aligned} \right.
f ( n ) f(n)

Solution


相当于一次可以走一步,可以走 π \pi 步,问从0走到 ( n 4 , n ] (n-4,n] 有几种走法
我们枚举走了1步的次数,枚举走了 π \pi 步的步数,组合数算就可以了

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

typedef long long LL;
const double pi=acos(-1);
const int MOD=1000000007;
const int N=2000005;

LL fac[N],ny[N];

int f(double x) {
	if (x>=0&&x<4) return 1;
	return (f(x-1)+f(x-pi))%MOD;
}

LL C(int n,int m) {
	if (n<m) return 0;
	return fac[n]*ny[m]%MOD*ny[n-m]%MOD;
}

LL ksm(LL x,LL dep) {
	LL res=1;
	for (;dep;dep>>=1) {
		(dep&1)?(res=res*x%MOD):0;
		x=x*x%MOD;
	}
	return res;
}

int main(void) {
	fac[0]=1; rep(i,1,N-1) fac[i]=fac[i-1]*i%MOD;
	rep(i,0,N-1) ny[i]=ksm(fac[i],MOD-2);
	int n; scanf("%d",&n);
	if (n<4) return 0&puts("1");
	LL ans=0;
	rep(i,0,n-4) {
		int tmp=(n-i-4)/pi;
		ans=(ans+C(tmp+i,i))%MOD;
	}
	rep(i,0,n-4) if (i*pi<=n-4) {
		int tmp=(int)(n-4-pi*i);
		ans=(ans+C(tmp+i,i))%MOD;
	}
	printf("%lld\n", ans);
	printf("%d\n", f(n));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/jpwang8/article/details/88126157
今日推荐