51nod1556 计算

ans[n]=ans[n-1]*3-m[n-2];YY一下可以懂的。减掉的就是往下走的情况不符合正整数的情况。m是默慈金数。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define ll long long
const int mod=1e9+7;
const int nmax=1e6+5;
ll inv[nmax],ans[nmax],m[nmax];
int main(){
	int n;scanf("%d",&n);
	inv[1]=1;
	rep(i,2,n+3) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
	m[1]=1;m[2]=2;
	rep(i,3,n) m[i]=((i*2+1)*m[i-1]%mod+3*(i-1)*m[i-2]%mod)%mod*inv[i+2]%mod;
	ans[1]=1;ans[2]=2;
	rep(i,3,n) ans[i]=(3*ans[i-1]%mod-m[i-2]+mod)%mod;
	printf("%d
",ans[n]);
	return 0;
}

  

基准时间限制:1 秒 空间限制:524288 KB 分值: 80 难度:5级算法题
 收藏
 关注
有一个1*n的矩阵 固定第一个数为1 其他填正整数  且相邻数的差不能超过1 求方案数%1e9+7的结果
Input
一个数n 表示1*n的矩阵(n<=10^6)
Output
一个数 表示方案数%1e9+7的结果
Input示例
3
Output示例
5
原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5866339.html