hdu 5225 Tom and permutation(回溯),hdupermutation

來源:互聯網
上載者:User

hdu 5225 Tom and permutation(回溯),hdupermutation

題目連結: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;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.