日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 6143 Killer Names(排列+容斥,dp)

發布時間:2025/5/22 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 6143 Killer Names(排列+容斥,dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Killer Names

HDU 6143 (容斥+排列組合,dp+整數快速冪) 2017ACM暑期多校聯合訓練 - Team 8 1011 Killer Names

題目鏈接

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1107 Accepted Submission(s): 545

Problem Description

Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith Lord Darth Vader. A powerful Force-user who lived during the era of the Galactic Empire, Marek originated from the Wookiee home planet of Kashyyyk as the sole offspring of two Jedi Knights—Mallie and Kento Marek—who deserted the Jedi Order during the Clone Wars. Following the death of his mother, the young Marek’s father was killed in battle by Darth Vader. Though only a child, Marek possessed an exceptionally strong connection to the Force that the Dark Lord of the Sith sought to exploit.

When Marek died in 2 BBY, shortly after the formation of the Alliance, Vader endeavored to recreate his disciple by utilizing the cloning technologies of the planet Kamino. The accelerated cloning process—an enhanced version of the Kaminoan method which allowed for a rapid growth rate within its subjects—was initially imperfect and many clones were too unstable to take Marek’s place as the Dark Lord’s new apprentice. After months of failure, one particular clone impressed Vader enough for him to hope that this version might become the first success. But as with the others, he inherited Marek’s power and skills at the cost of receiving his emotions as well, a side effect of memory flashes used in the training process.

— Wookieepedia

Darth Vader is finally able to stably clone the most powerful soilder in the galaxy: the Starkiller. It is the time of the final strike to destroy the Jedi remnants hidden in every corner of the galaxy.

However, as the clone army is growing, giving them names becomes a trouble. A clone of Starkiller will be given a two-word name, a first name and a last name. Both the first name and the last name have exactly n characters, while each character is chosen from an alphabet of size m. It appears that there are m2n possible names to be used.

Though the clone process succeeded, the moods of Starkiller clones seem not quite stable. Once an unsatisfactory name is given, a clone will become unstable and will try to fight against his own master. A name is safe if and only if no character appears in both the first name and the last name.

Since no two clones can share a name, Darth Vader would like to know the maximum number of clones he is able to create.

Input
The First line of the input contains an integer T (T≤10), denoting the number of test cases.

Each test case contains two integers n and m (1≤n,m≤2000).

Output
For each test case, output one line containing the maximum number of clones Vader can create.

Output the answer mod 109+7

Sample Input
2
3 2
2 3

Sample Output
2
18

題意:
有m種字符(可以不用完),組成兩個長度為n的字符串,要求這兩個字符串中不含有相同的字符。

求有多少種方式組成這兩個字符串。

dp求解
嘿嘿 在學姐博客復制過來的 學姐 博客
還是dp代碼短呀,就是不好想。

#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mod = 1e9 + 7; const int maxn = 2010; ll dp[maxn][maxn];ll quick_pow(ll a, ll b)///整數快速冪 {ll ans = 1;while (b){if (b & 1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans; }int main() {int T;scanf("%d",&T);while(T--){int n,m;scanf("%d%d",&n,&m);dp[1][1]=m;///第一個位置放置一種字符的方案數有且僅有一個for(int i=2; i<=n; i++)///從第二個位置遍歷到第n個位置{for(int j=1; j<=i&&j<=m; j++)///前i個位置可以選擇放的不同字符的個數最少為1個,最多為i個(每個位置上的字符都不一樣){/*要使前i個位置上有j個不同的字符,考慮兩種方案數1.前i-1個位置上就已經有了j個不同的字符了,那么第j個位置上就可以從這j個字符中任意選擇一個2.前i-1個位置上只有j-1個不同的字符,那么第j個位置上的字符就不能選擇已經選過的j-1個字符了,需要從剩余的(m-(j-1))個字符里面任意的選擇一個這兩種方案數共同構成*/dp[i][j]=(dp[i-1][j]*j%mod+dp[i-1][j-1]*(m-j+1)%mod)%mod;}}/*dp求出來的只是組成姓這n個字符的不同的方案數,我們還要考慮組成名的不同的方案數組成名的字符要從剩余的(m-j)個字符里面選擇,每個字符都對應著n中擺放位置,兩兩組合的方案數肯定是乘的關系最后將求得的結果累加*/ll ans=0;for(int j=1; j<m; j++){ans=(ans+dp[n][j]*quick_pow(m-j,n)%mod)%mod;}printf("%lld\n",ans);}return 0; }

分析:
容斥+排列組合

#include <bits/stdc++.h> #define siz 1005 #define maxn 2000 const long long mod = 1e9 + 7; typedef long long LL; using namespace std; int n, m; LL deposit[maxn + 5][maxn + 5], f[maxn + 5], comd[maxn + 5][maxn + 5]; void Init() {for(LL i = 1; i <= maxn; i++){deposit[i][0] = 1;for(int j = 1; j <= maxn; j++)deposit[i][j] = deposit[i][j - 1] * i % mod;///deposit[i]保存i的i的階乘}for(int i = 0; i <= maxn; i++){comd[i][0] = 1;for(int j = 1; j <= i; j++)comd[i][j] = (comd[i - 1][j] + comd[i - 1][j - 1]) % mod;///Comd[i]保存i的從i里面取j個的排列組合數的個數} } void solve() {LL sum = 0;memset(f, 0, sizeof(f));for(int i = 1; i <= m && i <= n; i++){sum = 0;for(int j = 1; j <= i; j++){sum = (sum + f[j] * comd[i][j] % mod) % mod; }f[i] = (deposit[i][n] - sum + mod) % mod;}LL ans = 0;for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){if(i + j > m) break;ans = (ans + (((f[i] * f[j]) % mod) * ((comd[m][i] * comd[m - i][j]) % mod)) % mod) % mod;///組合的方法}}printf("%lld\n", ans); } int main() {int T;Init();scanf("%d", &T);while(T--){scanf("%d %d", &n, &m);solve();}return 0; }

轉載于:https://www.cnblogs.com/nanfenggu/p/7900045.html

總結

以上是生活随笔為你收集整理的HDU 6143 Killer Names(排列+容斥,dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。