hdu 5225 Tom and permutation(回溯)
题目链接:hdu 5225 Tom and permutation
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll; const int maxn = 100;
const int mod = 1e9+7;
int N, ans, V[maxn + 5], A[maxn + 5];
ll S[maxn + 5], L[maxn + 5]; int query(int x) {
int ret = 0;
for (int i = 1; i < x; i++) {
if (V[i] == 0)
ret++;
}
return ret;
} int dfs(int d, int s) { if (d > N)
return 0; if (s == 0) {
ans = (ans + S[N-d+1]) % mod;
return L[N-d+1];
} else { int ret = 0; for (int i = 1; i <= A[d]; i++) {
if (V[i]) continue; V[i] = 1; int s = query(i);
ll temp = dfs(d + 1, i == A[d] ? 1 : 0);
ans = (ans + temp * s % mod) % mod; ret = (ret + temp) % mod; V[i] = 0;
} return ret;
}
} int main () {
S[1] = 0;
L[1] = 1;
for (int i = 2; i <= maxn; i++) {
S[i] = S[i-1] * i % mod+ L[i-1] * ((1LL * i * (i-1) / 2) % mod) % mod;
L[i] = L[i-1] * i % mod;
} while (scanf("%d", &N) == 1) {
ans = 0;
memset(V, 0, sizeof(V)); for (int i = 1; i <= N; i++)
scanf("%d", &A[i]); dfs(1, 1);
printf("%d\n", ans);
}
return 0;
}
hdu 5225 Tom and permutation(回溯)的更多相关文章
- HDU 5224 Tom and paper(最小周长)
HDU 5224 Tom and paper(最小周长) Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d &a ...
- HDU 5868 Different Circle Permutation(burnside 引理)
HDU 5868 Different Circle Permutation(burnside 引理) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=586 ...
- 组合数(Lucas定理) + 快速幂 --- HDU 5226 Tom and matrix
Tom and matrix Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5226 Mean: 题意很简单,略. analy ...
- HDU 5113 Black And White 回溯+剪枝
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...
- hdu 5224 Tom and paper
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5224 Tom and paper Description There is a piece of pa ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- [HDU 2553]--N皇后问题(回溯)/N皇后问题的分析
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2553 N皇后问题 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5225 枚举
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5225 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- HDU 2553 n皇后问题(回溯法)
DFS Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description ...
随机推荐
- spa(单页应用)中,使用history模式时,微信长按识别二维码在ios下失效的问题
spa(单页应用,vue)中,使用history模式时,微信长按识别二维码在ios下失效的问题. 触发条件: spa单页应用: 路由模式 history 从其他页面跳转到带有微信二维码识别的页面(不是 ...
- Linux 文件系统模型
声明:本文仅限于 cnblogs 发布,其他第三方网站均为盗版,原文地址:Linux 文件系统模型 在 Linux 环境下有过一些经历的同学可能都会遇到一个问题,这个问题就是往机器上插入 U盘 或者其 ...
- Servlet 学习笔记
Servlet 运行在服务器上的 java 类: Servlet 容器为 javaWeb 应用提供运行时环境,负责管理 servlet 和 jsp 生命周期,以及管理他们的共享数据. 现在我们知道了 ...
- Java反射机制使用场景
import java.io.*; import java.util.Properties; /*问题描述:存在一个主板--已经定义好,不想修改其代码,还想在主板上面增加一些其他功能? *问题解决方法 ...
- javascript 中遍历数组的简单方法
在Javascript中有自带方便遍历数组的方法(此方法非彼方法不要误会哦): 1 .利用for( index in array ){}; 2.利用 array.forEach( function(e ...
- spring+struts2+hibernate整合
web.xml需要配置 <context-param> <param-name>contextConfigLocation</param-name> <par ...
- 学会WCF之试错法——数据传输
数据传输 服务契约 [ServiceContract] public interface IService { [OperationContract] string GetData(int value ...
- springMVC+spring+MyBatis(SSM)的简单配置
SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中: Spring是一 ...
- typescript入门基础
1.typescript介绍 微软开发的一门编程语言,javascript的一个超集,遵循最新的ES6脚本语言规范(2015年发布),它扩展了Javascript的语法,任何已经写好的javascri ...
- 程序、计算机程序、java初论
一.程序? 程序一词来自生活,通常指完成某些事情的一种既定方式和过程,可以将程序看成对一系列动作的执行过程的描述. 例如:个人去银行取钱 1.带上存折/银行卡去银行 2.取号排队 3.将存折或储蓄卡递 ...