题意:
problem description
5 friends play LOL together . Every one should BAN one character and PICK one character . The enemy should BAN 5 characters and PICK 5 characters . All these 20 heroes must be different .
Every one can BAN any heroes by his personal washes . But he can only PICK heroes which he has bought .
Suppose the enemy can PICK or BAN any heroes. How many different ways are there satisfying the conditions?
For example , a valid way is :
Player 1 : picks hero 1, bans hero 2
Player 2 : picks hero 3, bans hero 4
Player 3 : picks hero 5, bans hero 6
Player 4 : picks hero 7, bans hero 8
Player 5 : picks hero 9, bans hero 10
Enemies pick heroes 11,12,13,14,15 , ban heroes 16,17,18,19,20 .
Input
The input contains multiple test cases.(No more than 20)
In each test case . there’s 5 strings S[1]∼S[5] ,respectively whose lengths are 100 , For the i-th person if he has bought the j-th hero, the j-th character of S[i] is ‘1’, or ‘0’ if not. The total number of heroes is exactly 100 .
Output
For each test case , print the answer mod 1000000007 in a single line .
样例输入
0110011100011001001100011110001110001110001010010111111110101010010011010000110100011001001111101011
1000111101111110110100001101001101010001111001001011110001111110101000011101000001011100001001011010
0100101100011110011100110110011100111100010010011001111110101111111000000110001110000110001100001110
1110010101010001000110100011101010001010000110001111111110101010000000001111001110110101110000010011
1000010011111110001101100000101001110100011000111010011111110110111010011111010110101111011111011011
样例输出
515649254
翻译成汉语,大概意思是LOL一共有100个英雄,十个人(每队5人)选英雄,每人可以选一个扳一个,问十个人有多少种不同的选扳方案(对答案取余1e9+7)。对面英雄全有,输入5个01串表示我方五个人分别拥有的英雄(1有0无)
注意点:1.每个人只能选自己拥有的英雄,任意两个人不能选或者扳同一个英雄。同一队伍的1,2,3,4,5召唤师选英雄1,2,3,4,5和选英雄5,4,3,2,1是不同的方案,而1,2,3,4,5召唤师扳英雄1,2,3,4,5和5,4,3,2,1是相同的方案
思路:
因为我们要求的是全部的方案数,所以我们不用管中间的过程(过程太复杂没法模拟),整体地看待总方案数为:我方选择的英雄方案数✖️敌人选择的英雄方案数✖️我方扳的英雄方案数✖️敌人扳的英雄方案数。
我们无论怎么选取5个英雄,那么敌人只能从剩下的95个中选5个,所以方案数是A(95,5),而我方扳的英雄方案数是C(90, 5),敌人扳的英雄方案数是C(85,5)。至于谁先谁后无所谓,因为:
A(95,5) * C(90,5) * C(85,5) = C(95,5) * C(90,5) * A(85,5) = C(95,5) * A(85,5) * C(90,5)
由于敌人选择的英雄方案数✖️我方扳的英雄方案数✖️敌人扳的英雄方案数的值是固定的,我们可以预处理出来,得到g,
因此我们只需要每次利用dp求解我方选择的英雄方案数,再乘g就能得到答案
关于dp求解我方选择的英雄方案数,我们可以设dp【i】【j】代表前j个英雄已经选了i个英雄的方案数,然后遍历每个成员,里层遍历每个英雄。为了避免重复,我们保证每个成员选的英雄都比上一个成员选的英雄下角标大(这样会导致方案数不全,因此我们需在最外层对成员下标进行全排列)
代码:
#include <stdio.h>
#include <inttypes.h>
#include <algorithm>
#include <string.h>
#define d int32_t
#define ll int64_t
#define mod 1000000007
#define r return
#define mem(a) memset(a, 0, sizeof(a));
#define For(i, star, endd) for(d i = star; i <= endd; i++)
using namespace std;
char s[8][150];
d t[8][150];
ll dp[8][150];
//扩展欧几里得
ll extend(ll a, ll b, ll &x, ll &y) {
if (a == 0 && b==0) r -1;
if (b == 0) {
x = 1;
y = 0;
r a;
}
ll g = extend(b, a % b, y, x);
y -= a / b * x;
r g;
}
//求逆元
ll mod_re(ll a, ll n) {
ll x, y;
ll g = extend(a, n, x, y);
if(g == 1) r (x % n + n) % n;
r -1;
}
//求A(95,5) * C(90, 5) * C(85, 5)的值
ll init() {
ll res = 1;
For(i, 81, 95) {
res = res * i % mod;
}
For(i, 2, 5) {
ll g = mod_re(i, mod);
res = res * g % mod * g % mod;
}
r res;
}
//求我方选择英雄的总类数
ll work() {
ll ans =0;
d inx[5] = {0, 1, 2, 3, 4};
do {
mem(dp);
For(j, 0, 99) {
dp[0][j] = dp[0][j - 1];
if(s[inx[0]][j] == '1') {
dp[0][j]++;
}
}
For(i, 1, 4) {
For(j, 0, 99) {
dp[i][j] = dp[i][j - 1];
if(s[inx[i]][j] == '1') {
dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % mod;
}
}
}
ans = (ans + dp[4][99]) % mod;
}while (next_permutation(inx, inx + 5));
r ans;
}
int main() {
ll g = init();
while (scanf("%s", s[0]) == 1) {
For(i, 1, 4) {
scanf("%s", s[i]);
}
ll ans = work();
printf("%lld\n", g * ans % mod);
}
r 0;
}
如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢