#include<bits/stdc++.h>

#define mod 1000000007

using namespace std;

typedef long long ll;

ll moni[1000005];

ll exgcd(ll a, ll b, ll &x, ll &y)

{

    if (b == 0)

    {

        x = 1;

        y = 0;

        return a;

    }

    ll r = exgcd(b, a % b, x, y);

    ll t = x % mod;

    x = y % mod;

    y = ((t - a / b * y) % mod + mod) % mod;

    return r;

}

ll inv[1000005];

void init()

{

    inv[1] = 1;

    for(int i = 2; i < 1000002; i ++)

        inv[i] = (mod - mod / i) * 1ll * inv[mod % i] % mod;

moni[1]=1;

    moni[2]=2;

    for(int i=3;i<=1000000;i++)

        moni[i]=((((2*i+1)*moni[i-1]%mod+1ll*3*(i-1)*moni[i-2]%mod)%mod)*inv[i+2]%mod)%mod;

}

ll ans[1000005];

int main()

{

    #ifndef ONLINE_JUDGE

        //freopen("in.txt","r",stdin);

    #endif // ONLINE_JUDGE

    init();

    int n;

    cin>>n;

    ans[1]=1;

    ans[2]=2;

    for(int i=3;i<=n;i++)

        ans[i]=(1ll*ans[i-1]*3%mod-moni[i-2]+mod)%mod;

    cout<<ans[n]<<endl;

    return 0;

}

51 Nod 1556计算(默慈金数的应用)的更多相关文章

  1. 51nod1556 计算(默慈金数)

    Problem 有一个\(1*n\)的矩阵,固定第一个数为\(1\),其他填正整数, 且相邻数的差不能超过\(1\),求方案数. \(n\le 10^6\) Solution 容易发现答案是\(f_n ...

  2. hdu5673 Robot 卡特兰数 / 默慈金数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5673 分析: 这道题是一道裸的默慈金数,比较容易想到的是用卡特兰数来做.不了解的可以先学习一下. 卡特 ...

  3. HDU5673 Robot 默慈金数

    分析: 注:然后学了一发线性筛逆元的姿势 链接:http://blog.miskcoo.com/2014/09/linear-find-all-invert #include<iostream& ...

  4. 51 nod 1105 第K大的数

    1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...

  5. hdu-5673 Robot(默次金数)

    题目链接: Robot Time Limit: 12000/6000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Others) 问题描述 ...

  6. 51 nod 1097 拼成最小的数 思路:字符串排序

    题目: 思路:1.以字符串输入这些整数. 2.对这些字符串排序,排序规则为尽量让能让结果变小的靠前. 代码中有注释,不懂的欢迎在博客中评论问我. 代码: #include <bits\stdc+ ...

  7. 51nod 1556 计算(递推)

    传送门 解题思路 在一个网格图上走\(n\)步,每次可以向右上,右下,右,但必须在第一象限,最后从\((0,0)\)走到\((n,0)\)的方案数为默慈金数.递推式为\(m[i+1]=\frac{(2 ...

  8. 51 nod 1394 1394 差和问题(线段树)

    1394 差和问题基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个多重集合S(即里面元素可以有重复),初始状态下有n个元素,对他进行如下操作: 1.向S里面添 ...

  9. 51 nod 1188 最大公约数之和 V2

    1188 最大公约数之和 V2 题目来源: UVA 基准时间限制:2 秒 空间限制:262144 KB 分值: 160 难度:6级算法题   给出一个数N,输出小于等于N的所有数,两两之间的最大公约数 ...

随机推荐

  1. postman测试webservice接口

  2. Python 过滤HTML实体符号简易方法

    html_tag = {' ': '\n', '"': '\"', '&': '', '<': '<', '>': '>', '&apos; ...

  3. 终于搞懂了vue 的 render 函数(一) -_-|||

    终于搞懂了vue 的 render 函数(一) -_-|||:https://blog.csdn.net/sansan_7957/article/details/83014838 render: h ...

  4. python面向对象反射-框架原理-动态导入-元类-自定义类-单例模式-项目的生命周期-05

    反射 reflect 反射(reflect)其实是反省,自省的意思 反省:指的是一个对象应该具备可以检测.修改.增加自身属性的能力 反射:通过字符串获取对象或者类的属性,进行操作 设计框架时需要通过反 ...

  5. python 写接口供外部调用

    .py: import requests import urllib2 import commands import subprocess def check(): status, msg = com ...

  6. CentOS 7 防火墙常用操作及常见问题处理

    一.常用操作 1.启动防火墙: systemctl start firewalld.service 2.关闭防火墙: systemctl stop firewalld.service 3.添加放行端口 ...

  7. 怎么快速写好看的手机menu菜单

    要达到这样的效果: <div class="menu"> <div class="menu-1"> <img alt=" ...

  8. tomcat启动报ClassNotFound

    排除本来就缺少该类的原因,经过自己经验和网上查的资料,解决方式如下: jar包冲突(关闭其他项目) eclipse的java版本不对,点击项目,右键properties在project facets, ...

  9. @RequestMapping-占位符映射

    占位符映射

  10. linux查看端口被占用情况

    Linux 查看端口占用情况可以使用 lsof 和 netstat 命令. 如果linux中没有这两个命令,则yum安装一下 yum install -y lsof yum install -y ne ...