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 ...
随机推荐
- centos6.7系统安装流程
虚拟机创建centos的过程,如下: 1.首先创建一个空白文件 2.打开虚拟机,打开文件,或者页面的<创建虚拟机>,如下: 3.打开之后如下所示,选择自定义,Linux崇尚自由 4.第四步 ...
- SUID,SGID,SBIT这些到底是什么
SUID,SGID,SBIT这些都是文件的特殊权限. SUID(Set UID)文件执行过程中,用户拥有文件的root权限. SGID(Set GID)文件执行过程中,执行者拥有该文件的用户组的权限. ...
- 【分享】jQuery无插件实现 鼠标拖动图片切换 功能
前言 我就想随便叨逼叨几句,爱看就看几句,不爱看就直接跳过看正文就好啦~ 这个方法是仿写页面时我自己研究出来,可能有比我更简单的方法. 但我不管,因为我没查我不知道,我就觉得我的最好啦,耶耶耶~ 效果 ...
- java变量与内存深入了解
========================================================================================= 在我看来,学习jav ...
- Golang 任务队列策略 -- 读《JOB QUEUES IN GO》
Golang 在异步处理上有着上佳的表现.因为 goroutines 和 channels 是非常容易使用且有效的异步处理手段.下面我们一起来看一看 Golang 的简易任务队列 一种"非任 ...
- python中print()函数的“,”与java中System.out.print()函数中的“+”
python中的print()函数和java中的System.out.print()函数都有着打印字符串的功能. python中: print("hello,world!") 输出 ...
- Javascript日期类型的妙用
http://heeroluo.net/Article/Detail/110 获取某个月份的天数 相信大家读小学的时候就知道一年十二个月各有多少天了,这里面有个特殊的存在——2月.闰年的2月有29天, ...
- JVM菜鸟进阶高手之路十四:分析篇
转载请注明原创出处,谢谢! 题目回顾 JVM菜鸟进阶高手之路十三,问题现象就是相同的代码,jvm参数不一样,表现的现象不一样. private static final int _1MB = 1024 ...
- jquery.form.js+jquery.validation.js实现表单校验和提交
一.jquery引用 主要用到3个js: jquery.js jquery.form.js jquery.validation.js 另外,为了校验结果提示本地化,还需要引入jquery.vali ...
- Android UsageStatsService(应用使用统计服务)的学习与调研
一. 简介 UsageStatsService是一个系统服务,其主要通过AMS等,来监测并记录各个应用的使用数据,如上次调用com.android.settings的时间等. UsageStatsSe ...