在oeis上搜了这么个公式,加上乘法逆元就AC了,不明觉厉。。。http://oeis.org/A005773
题目链接:http://www.51nod.com/contest/problem.html#!problemId=1556
// 计算.cpp
#include <bits/stdc++.h>
#define LL long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define PI 3.1415926535897932626
#define EXIT exit(0);
#define PAUSE system("pause");
#define DEBUG puts("Here is a BUG");
#define SYNC_CLOSE ios::sync_with_stdio(false);
#define what_is(x) cout << #x << " is " << x << endl;
#define CLEAR(name, init) memset(name, init, sizeof(name));
const double eps = 1e-8;
const int MAXN = (int)1e6 + 5;
const LL MOD = 1000000007LL;
using namespace std;
LL a[MAXN];
void ex_gcd(LL a, LL b, LL &x, LL &y) {
if (b == 0) {
x = 1; y = 0;
return;
}
ex_gcd(b, a % b, y, x);
y -= x * (a/b);
return;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
freopen("D:\\Documents\\Disk_Synchronous\\Programs\\Acm\\input.txt", "r", stdin);
#endif
int n; cin >> n;
a[1] = 1; a[2] = 2;
for(int i = 3; i <= n; i++) {
a[i] = 2*a[i-1] % MOD;
LL x, y;
ex_gcd(i, MOD, x, y);
x = (x + MOD) % MOD;
a[i] = (a[i] + 3*(i-2)*a[i-2]%MOD*x%MOD) % MOD;
}
cout << a[n] << endl;
return 0;
}