51nod 算法马拉松11 D 计算

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值