2017 ICPC semi-finals (Xi'an station) --- J-question LOL (DP), icpclol
Question Link
Problem description
5 friends play LOL together. Every one shoshould BAN one character and PICK one character. The enemy shoshould BAN 55 characters and PICK 55 characters. All these 2020 heroes must be different.
Every one can BAN any heroes by his personal es. 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 11: picks hero 11, bans hero 22
Player 22: picks hero 33, bans hero 44
Player 33: picks hero 5, bans hero 66
Player 44: picks hero 77, bans hero 88
Player 55: picks hero 99, bans hero 1010
Enemies pick heroes 11, 12, 13, 14, 1511,12, 13, 14, 15, ban heroes 16, 17, 18, 19, 18, 19, 20.
Input
The input contains multiple test cases. (No more than 2020)
In each test case. there's 55 strings s [1] \ sim S [5] S [1] ∼ S [5], respectively whose lengths are 100100, for the ii-th person if he has bought the jj-th hero, the jj-th character of S [I] S [I] is '11 ', or '00' if not. the total number of heroes is exactly 100100.
Output
For each test case, print the answer mod 10000000071000000007 in a single line.
Sample Input
01100111000110010011000111100011100011100010100101111111101010100100110100001101000110010011111010111000111101111110110100001101001101010001111001001011110001111110101000011101000001011100001001011010010010110001111001110011011001110011110001001001100111111010111111100000011000111000011000110000111011100101010100010001101000111010100010100001100011111111101010100000000011110011101101011100000100111000010011111110001101100000101001110100011000111010011111110110111010011111010110101111011111011011
Sample output
515649254
Question Source
ACM-ICPC 2017 Asia Xi'an
Question: The meaning of the question is messy. In short, it is to input five '0' '1' strings with a length of 100, we need to retrieve a '1' from each row, but these five '1' must be in different columns. How many methods are there? Note that the output result is multiplied by the constant tmp = 531192758
Idea: we can solve this problem with DP, dp [I] [j] = (dp [I] [j] + dp [I-1] [J-1]) % mod ensures that the current row obtains a '1' following the '1' of the previous row, and then sorts the five strings in an inverted order, sum up the dp values in all the arranged Order (dp [5] [100 ]).
The positive solution for this question is to compress DP complexity O (n) = 2 ^ 5*500, and my above DP complexity O (n) = 5! * 500. During the on-site competition, most people experienced brute force attacks ~ At that time, I was dumb. I thought it was not feasible for DP half of my thoughts. After the game, I thought it was feasible again. The competition was a little stressful, especially in the last half hour.
The Code is as follows:
# Include <iostream> # include <algorithm> # include <stdio. h ># include <cstring> using namespace std; typedef long LL; const LL mod = 1e9 + 7; char s [6] [105]; LL dp [6] [105], ans; void Backtrack (int t) {if (t = 5) {memset (dp, 0, sizeof (dp )); for (int I = 1; I <= 100; I ++) {dp [1] [I] = dp [1] [I-1]; if (s [1] [I] = '1') dp [1] [I] ++;} for (int I = 2; I <= 5; I ++) {for (int j = 1; j <= 100; j ++) {dp [I] [j] = dp [I] [J-1]; if (s [I] [j] = '1') dp [I] [j] = (Dp [I] [j] + dp [I-1] [J-1]) % mod;} ans = (ans + dp [5] [100]) % mod; return;} for (int I = t; I <= 5; I ++) {swap (s [I], s [t]); Backtrack (t + 1 ); swap (s [I], s [t]) ;}} int main () {LL tmp = 531192758; // constant tmp = A () * C) * C (85); while (scanf ("% s", s [1] + 1 )! = EOF) {for (int I = 2; I <= 5; I ++) scanf ("% s", s [I] + 1); ans = 0; backtrack (1); printf ("% lld \ n", ans * tmp % mod);} return 0 ;}