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 ...
随机推荐
- Problem H: STL——括号匹配
Description 给出一堆括号,看其是否匹配,例如 ().()().(()) 这样的括号就匹配, )(.)()) 而这样的括号就不匹配 Input 每一行代表一组测试样例,每组测试样 ...
- MySQL数据库中文变问号
原文参考:http://www.linuxidc.com/Linux/2017-05/144068.htm 系统是的Ubuntu 16,修改以下配置 1.sudo vi /etc/mysql/my. ...
- How to Quickly Create a Copy of a Table using Transact-SQL
The easiest way to create a copy of a table is to use a Transact-SQL command. Use SELECT INTO to ext ...
- 实现一个websocket服务器-理论篇
本文是Writing WebSocket servers的中文文档,翻译自MDNWriting WebSocket servers.篇幅略长,个人能力有限难免有所错误,抛砖引玉共同进步. websoc ...
- C#实现设置完整虚拟路径
){ mHttpUrl.Append(":"); mHttpUrl.Append(port);}string mServerName = "~/AppModu ...
- [转载] Java集合---HashMap源码剖析
转载自http://www.cnblogs.com/ITtangtang/p/3948406.html 一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射 ...
- shell全自动登录远程终端
先看效果 你需要做的事情,在配置文件中配置服务器信息,选择对应的服务器,进行连接. 传统手工连接 #密码方式 ssh user@ip # 然后输入服务器密码 #密钥登录 ssh -i identity ...
- Redis使用说明详解
原博主地址:http://www.cnblogs.com/wangyuyu/p/3786236.html Redis使用详细教程 一.Redis基础部分: 1.redis介绍与安装比mysql快10倍 ...
- MVC-1(javabean+jsp+servlet+jdbc)
这是一篇最初版本的mvc设计模式的demo.路要一步一步走,弄明白这其中的逻辑,对后面掌握ssh,ssm等框架大有裨益. 计算机系的同学们也要为毕设做准备了,希望可以帮你们迈出自己做毕设的第一步(微笑 ...
- 企业级应用TOMCAT
第1章 Tomcat 1.1 Tomcat简介 Tomcat是一个免开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不多的场合下被普遍使用,是开发调试JSP程序的首选,另 ...