UVa 10616 divisible Group Sums:dfs and DP

Source: Internet
Author: User
Tags sort time limit


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/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.