HDU 5355 Cake(数学 DFS)

来源:互联网 发布:中国大学生软件杯 编辑:程序博客网 时间:2024/06/08 04:06

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355


Problem Description
There are m soda and today is their birthday. The 1-st soda has prepared n cakes with size 1,2,,n. Now 1-st soda wants to divide the cakes into m parts so that the total size of each part is equal. 

Note that you cannot divide a whole cake into small pieces that is each cake must be complete in the m parts. Each cake must belong to exact one of m parts.
 

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first contains two integers n and m (1n105,2m10), the number of cakes and the number of soda.
It is guaranteed that the total number of soda in the input doesn’t exceed 1000000. The number of test cases in the input doesn’t exceed 1000.
 

Output
For each test case, output "YES" (without the quotes) if it is possible, otherwise output "NO" in the first line.

If it is possible, then output m lines denoting the m parts. The first number si of i-th line is the number of cakes in i-th part. Then si numbers follow denoting the size of cakes in i-th part. If there are multiple solutions, print any of them.
 

Sample Input
41 25 35 29 3
 

Sample Output
NOYES1 52 1 42 2 3NOYES3 1 5 93 2 6 73 3 4 8
 

Source
2015 Multi-University Training Contest 6

题意:

1 到 n 的数字,要分为 m 组,每组的和要相等!输出分配方案!


PS:(转)

首先可以求出这些蛋糕的总和n(n+1)/2,如果总和sum%m != 0那么就不肯能被平分成m份,那么输出”NO”。 
接下来计算平均数avg=sum/m,如果平均数avg < n的话,蛋糕是不可能用完的,同样也输出”NO”。 
剩下的情况蛋糕是一定能拼成”YES”的,那么可以将这些蛋糕以2*m为单位一组一组的分配,每个人拿当前这组的最大和最小,次大和次小….. 
直至拿到剩下[0,4m]个蛋糕之间是停止。 
这时候进行暴力求解,问题变成了1~n个数字,能否拼出,和相同的m个数字,这里直接暴力搜索dfs。


代码如下:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>using namespace std;typedef long long LL;#define maxn 100000+17vector<int >ans[17];//ans[i]:每行放置的数字LL n, m;LL SUM[17];int vis[maxn], ma[maxn];LL rest_sum, res;void init(){    memset(ma,  0, sizeof(ma));    memset(vis, 0, sizeof(vis));    memset(SUM, 0, sizeof(SUM));    for(int i = 0; i < 17; i++)    {        ans[i].clear();    }}int DFS(int r, int tsum)//第几行、当前和{    if(r == m+1)    {        return 1;    }    for(int i = res; i >= 1; i--)    {        if(vis[i])        {            continue;        }        if(tsum+i == rest_sum)        {            ma[i] = r;            vis[i] = 1;            if(DFS(r+1, 0))            {                return 1;            }            vis[i] = 0;        }        else if(tsum + i < rest_sum)        {            ma[i] = r;            vis[i] = 1;            if(DFS(r, tsum+i))            {                return 1;            }            vis[i] = 0;        }    }    return 0;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        init();        scanf("%lld %lld",&n, &m);        LL sum = n*(n+1)/2;        if(sum % m)//数字的和不能整除m,那么每行就不可能达到和相等        {            printf("NO\n");            continue;        }        if(n < 2*m-1)//每行的和必须大于等于n:sum/m >= n        {            printf("NO\n");            continue;        }        //res为每行2个一组后多余的数        res = n%(2*m);//每行2个数分为一组        if(res)//不能每组个数相同        {            res += 2*m;//最后一组有res+2*m个数            res = min(n, res);//防止res比n还大 例如:3 2这组数据        }        for(int i = n; i > res; i-=(2*m))//n 到 res的数        {            for(int j = 1; j <= m; j++)            {                int x = i-j+1;                int y = i-2*m+j;                ans[j].push_back(x);                ans[j].push_back(y);                SUM[j] += x;                SUM[j] += y;//第j行目前的和            }        }        LL avg = sum/m;        rest_sum = avg - SUM[1];//每行还差rest_sum达到的avg(每行的目标和)        DFS(1, 0);//1 到res的数 怎样放置在每行才能和相同(和==rest_sum)        for(int i = 1; i <= res; i++)        {            ans[ma[i]].push_back(i);        }        printf("YES\n");        for(int i = 1; i <= m; i++)        {            printf("%d",ans[i].size());            for(int j = 0; j < ans[i].size(); j++)            {                printf(" %d",ans[i][j]);            }            printf("\n");        }    }    return 0;}/*991 25 35 29 326 3100 5*/


1 0