10616-divisible Group sums
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_ problem&problem=1557
Idea: Use dfs+ memory to search the enumeration combination, notice the number has <0.
return dp[i][cnt][sum] = f (i + 1, cnt + 1, ((sum + arr[i])% d + D)% d) + f (i + 1, cnt, sum);///i<n,cnt<m
Complete code:
/*0.049s*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int M = 30 + 5;
const int N = 30 * 30 + 5;
ll Rb [M] [N], Rw [M] [N], B [M], W [M]; /// B = Black, W = White
void calc (int n, int k, ll R [] [N], ll C [])
{
int i, j;
memset (R, 0, sizeof (R));
for (i = 0; i <= n; i ++)
R [i] [0] = 1;
for (i = 1; i <= n; i ++)
for (j = 1; j <= C [i]; j ++)
R [i] [j] = R [i-1] [j] + R [i-1] [j-1] * (C [i]-j + 1);
}
int main ()
{
int i, j, n, k;
ll ans;
while (scanf ("% d% d", & n, & k), n)
{
memset (B, 0, sizeof (B));
memset (W, 0, sizeof (W));
for (i = 1; i <= n; i ++)
for (j = 1; j <= n; j ++)
{
if ((i + j) & 1) ++ W [(i + j) >> 1];
else ++ B [(i + j) >> 1]; /// Calculate the number of diagonal grids of black and white, so that you do not need to consider the parity of n
}
sort (B + 1, B + n + 1);
sort (W + 1, W + n);
calc (n, k, Rb, B);
calc (n-1, k, Rw, W); /// Calculate the number of schemes on the white grid and the black grid, respectively
ans = 0;
for (i = 0; i <= k; i ++)
ans + = Rb [n] [i] * Rw [n-1] [k-i];
printf ("% lld \ n", ans);
}
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/