Cake ZQUOJ 25779

Cake

  • Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others)
  • Total Submission(s): 5     Accepted Submission(s): 2
  • Special Judge
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 (1 ≤ n ≤ 105,  2 ≤ m ≤ 10), 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

4
1 2
5 3
5 2
9 3

Sample Output

NO
YES
1 5
2 1 4
2 2 3
NO
YES
3 1 5 9
3 2 6 7
3 3 4 8

Source
2015 Multi-University Training 6



题意:给你n个尺寸大小分别为1,2,3,…,n的蛋糕,要求你分成m份,要求每份中所有蛋糕的大小之和均相同,如果有解,输出“YES”,并给出每份的蛋糕数及其尺寸大小,否则输出“NO”

例如n=5,m=3,即大小尺寸分别为1,2,3,4,5的5个蛋糕,要求分成三份,那么解可以是第一份一个蛋糕,大小为5;第二份两个蛋糕,大小为1、4;第三份两个蛋糕,大小为2、3。这样每份大小之和均为5,满足题目要求。

放上出题人的解题报告



放上出题人的解题报告



解题思路:此题无解的情况无外乎两种:①所有蛋糕的大小之和无法被m整除;②每一份蛋糕的大小之和不得小于蛋糕大小的最大值n,即sum/m>=n,转化一下为n*m<=sum,亦可以将sum代入,转化为n>=2*m-1。

之后便是有解的情况,有解的情况每份蛋糕的大小之和是知道的,即sum/m。然后我们可以知道n最小得是2*m-1,举个例子就很清楚了,譬如n=5,m=3,

5 3

1 5

2 4 1

2 3 2

2*m-1就说明了n本身可以自己一组,其他两两一组,一个正向递增,一个反向递减,必定可以凑成n,当然这是奇数的情况

但该方法其实是不分奇偶性的,上述只是说明。其实我们需要做的就是把分配方式分成两部分,一部分是k个2*m,另外就是剩下的那部分,我们来看看n=23,m=6这组数据,之前AC的代码普遍过不了这组数据

23 6
YES
11      12  23
10  1  13  22
4   9  2  14  21
4   8  3  15  20
4   7  4  16  19
4   6  5  17  18

先利用(n-m*2+1)%(m*2)+m*2-1算出粉色那部分数据的大小,然后利用贪心的思想二分一下即可找到符合的蛋糕大小,剩下的绿色部分则是k个2*m的部分,该部分小的数是正向递增的,大的数是反向递减的,所以必定是符合要求的。为了更加直观,放上n=100,m=5的数据

100 5
YES
20  10  1  11  100  12  99  13  98  14  97  15  96  16  95  17  94  18  93  19  92
20    9  2  20    91  21  90  22  89  23  88  24  87  25  86  26  85  27  84  28  83
20    
8  3  29    82  30  81  31  80  32  79  33  78  34  77  35  76  36  75  37  74
20    
7  4  38    73  39  72  40  71  41  70  42  69  43  68  44  67  45  66  46  65

20    6  5  47    64  48  63  49  62  50  61  51  60  52  59  53  58  54  57  55  56

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,r;
    scanf("%d",&t);
    while(t--)
    {
        long long n,m;
        scanf("%lld %lld",&n,&m);
        if((n*(n+1)/2)%m||2*m-1>n)
            printf("NO\n");
        else
        {
            printf("YES\n");
            set<int> v;
            set<int>::iterator it;
            int w[32];
            int c=(n-2*m+1)%(2*m)+2*m-1;
            int s=(c*(c+1))/(2*m);
            int d=(n-c)/(2*m);
            for(int i=1;i<=c;i++)
                v.insert(i);
            for(int j=0,k=c+1;j<m;j++,putchar('\n'))
            {
                for(r=c=0;r<s;)
                {
                    it=v.upper_bound(s-r);
                    r+=w[c++]=*--it;
                    v.erase(it);
                }
                printf("%d",c+d*2);
                for(int i=0;i<c;i++)
                    printf(" %d",w[i]);
                for(int i=0;i<d;i++)
                    printf(" %d %d",k++,n--);
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值