In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element appears in its original position.

There's originally an array consisting of n integers from 1 to n in ascending order, you need to find the number of derangement it can generate.

Also, since the answer may be very large, you should return the output mod 109 + 7.

Example 1:

Input: 3
Output: 2
Explanation: The original array is [1,2,3]. The two derangements are [2,3,1] and [3,1,2].

Note:
n is in the range of [1, 106].

思路:

第一反应是挨个求全排列,判断是否符合,结果果断超时,当n=12的时候就已经极限了。

bool isOrder(vector<int>& a,int n)
{
for(int i =;i<n;i++)
{
if(a[i]== (i+))return false;
}
return true;
} int findDerangement2(int n)
{
if(n==)return ;
if(n==)return ;
//if(n==3)return 2;
vector<int> ori(n,); for(int i =;i<n;i++)ori[i]=i+; long long ret=;
while(next_permutation(ori.begin(),ori.end()))
{
if( isOrder(ori,n) ) ret++;
}
return ret %;
}

结果想,这么做肯定不多,就想到了用动态规划。

仔细观察,

3的结果为: 2 3 1 与 3 1 2,

求4的结果的时候,将数字4分别与 3的结果每一位进行交换肯定满足题意,即将4与2 3 1和3 1 2每一位交换,得到

4 3 1 2

2 4 1 3

2 3 4 1

4 1 2 3

3 4 2 1

3 1 4 2

一共6个即 dp[3]*3.

还有一种情况就是 3个排列比如

1 X X

X 2 X

X X 3

其中X X是2的排列,将数字 1 2 3分别与4交换,肯定也满足题意 即 d[2]*3

综上就得到 dp[4]= dp[3]*3+d[2]*3;

dp[n] = ((i-1)*dp[i-1]+(i-1)*dp[i-2] )%1000000007;

但是程序不能这么写。。因为((i-1)*dp[i-1]+(i-1)*dp[i-2] )亲测溢出。。。。

改写成dp[n]=(i-1)*(dp[i-1]+dp[i-2])%1000000007;

vector<long long> dp(n+,);
dp[]=,dp[]=,dp[]=;
for(int i =;i<=n;i++)
{
dp[i] = (i-)*(dp[i-]+dp[i-])%;
}
return dp[n];

[leetcode-634-Find the Derangement of An Array]的更多相关文章

  1. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

  2. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  3. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  4. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  5. 乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array

    乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array 一.前言     将传统的问题进行一些稍微的变形,这个时候我们可能无所适从了,因此还是实践出真知, ...

  6. 乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array

    乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array 一.前言     我们这次的实验是去除重复的有序数组元素,有大体两种算法. 二.Remo ...

  7. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  8. LeetCode——Find All Numbers Disappeared in an Array

    LeetCode--Find All Numbers Disappeared in an Array Question Given an array of integers where 1 ≤ a[i ...

  9. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

  10. [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

随机推荐

  1. spring(二)-反射、动态代理

    主要是对上一篇文章中涉及到的点做补充,欢迎指正! 1.  java反射知识-Spring IOC 依赖注入 Java反射机制主要提供了以下功能: 在运行时判断任意一个对象所属的类:在运行时构造任意一个 ...

  2. Openresty最佳案例 | 第8篇:RBAC介绍、sql和redis模块工具类

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616738 本文出自方志朋的博客 RBAC介绍 RBAC(Role-Based Acce ...

  3. Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)

    上篇文章中实现了基本的打包功能,在这篇我们来解决不同平台打AB包的问题. 本篇文章的核心api还是: BuildPipeline.BuildAssetBundles (outPath, 0, Edit ...

  4. Xshell中使用FTP/SFTP工具下载文件

    (1)sftp host_ip,输入用户名/密码 (2)通过cd命令找到远程服务器要拷贝的文件: 通过lcd命令指定本地保存地址. (3)通过get filename拷贝文件 (4)在本地查看,已经可 ...

  5. Oracle之视图

    Oracle之视图 2018.9.12 由于视图的数据与表数据互相关联,所以切记谨慎操作 建立视图 使用下面sql语句来完成视图的创建 create or replace view 视图名 as se ...

  6. Python基础—12-面向对象(02)

    面向对象 面向对象三大特点 封装:既可对数据结构进行封装,又可对处理数据的方法进行封装 继承:强调的父子类的关系 多态:不同对象调用相同的方法,会有不同的响应 类的继承 相关概念 继承:父类的属性和方 ...

  7. jdk下载安装

    1.下载地址:https://www.oracle.com 注册,登陆,选择版本下载(注意,下载时提示you must accept the license agreement before down ...

  8. 【2018 ICPC焦作网络赛 K】Transport Ship(多重背包二进制优化)

    There are N different kinds of transport ships on the port. The ith kind of ship can carry the weigh ...

  9. springboot的aop编程

    以下内容是模仿杨开振<<深入浅出springboot 2.x>>的4.2章节内容. 开始前,需要先修改pom.xml,加入以下内容 <!-- https://mvnrep ...

  10. 阿里云SSL证书到期(续期)图文教程

    今天公司项目突然报错 后来查询是SSL证书过期了.友情提示: 证书产品仅支持新签发.不支持续费.证书到期前需在阿里云SSL证书控制台重新购买和申请证书. 登录阿里云控制台,点击产品与服务,在搜索框搜索 ...