HDU - 6616 Divide the Stones

Posted evlpsrfc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 6616 Divide the Stones相关的知识,希望对你有一定的参考价值。

Divide the Stones

??传送门

Time Limit: 6000/3000 MS (Java/Others)

Memory Limit: 131072/131072 K (Java/Others)

Problem Description

There are n stones numbered from 1 to n.
The weight of the i-th stone is i kilograms. We divide the stones into k groups.
Each group consists of exactly stones.
We define the weight of each group is sum of the stones’ weights in the group.
Can we divide the stones so that the weights of all groups are same?

Input

The first line of input contains an integer T (1 <= T <= 100) denoting the number of test cases.
Each test case consists of one line containing two integers n (1 ≤ n ≤ 100000), k (k is divisor of n).
It is guaranteed that the sum of n overall test cases does not exceed 500000.

Output

For each test case, if you can’t divide into k groups satisfying condition, print “no”.
Else if you can divide into k groups satisfying condition, print “yes” in one line and then print k lines.
The i-th line represent the indices of stones belonging to the i-th group.
If there are multiple solutions, you can print any of them.

Sample Input

1
4 2

Sample Output

yes
1 4
2 3

 Tips

题意

有 $n$ 颗石子,第 $i$ 颗重量为 $i$ 。现在请你将这 $n$ 颗石子均分成 $k$ 堆,每堆的重量和石子数量都一样。输出任意一种均分方式,如果不可以,输出 no 。

题解

首先来说,当 $k \nmid n(n+1)/2$ 时一定无解。

然后我们先考虑 $n/k$ 为偶数时的情形。

此时,每一堆个数为偶数,重量为 $(n+1)n/k/2$ ,则可以用 $n/k/2$ 个和为 $n+1$ 的数对组成。

然后考虑 $n/k$ 为奇数的情况。

此时,我们可以先将前 $3k$ 颗石子按照同等重量和数量分成 $k$ 堆,即每堆 $3$ 个,重量 $(9k+3)/2$ 。

然后,我们只需将剩下的部分成对分配即可。

关于 $3k$ 分成 $k$ 堆的方式,可能分法有多种,下面给出一种比较容易想到的:

若 $i$ 是奇数,则分为 $$i,\frac3k+22-\fraci2,\frac6k+12-\fraci2$$

否则,分为 $$i,\frac4k+22-\fraci2,\frac5k+12-\fraci2$$

技术图片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int T,n,k;
 4 int main()
 5     scanf("%d",&T);
 6     while (T--)
 7         scanf("%d%d",&n,&k);
 8         if (1LL*n*(n+1)/2%k)
 9             puts("no");
10         
11         else
12             puts("yes");
13             if (k==1)
14                 putchar(1);
15                 for (int i=2;i<=n;++i)
16                     printf(" %d",i);
17                 puts("");
18             
19             else if ((n/k)&1)
20                 int i=1,j=3*k+1,kk=k;
21                 while (kk--)
22                     if (i&1)
23                         printf("%d %d %d",
24                                i++,(3*k+1)/2-i/2,(6*k+1)/2-i/2);
25                     else
26                         printf("%d %d %d",
27                                i++,(4*k+2)/2-i/2,(5*k+1)/2-i/2);
28                     for (int i=0;i<(n/k-3)/2;++i)
29                         printf(" %d %d",j++,3*k+1+n-j);
30                     puts("");
31                 
32             
33             else
34                 int i=1,kk=k;
35                 while (kk--)
36                     printf("%d %d",i++,n+1-i);
37                     for (int j=1;j<n/k/2;++j)
38                         printf(" %d %d",i++,n+1-i);
39                     puts("");
40                 
41             
42         
43     
44     return 0;
45 
View Code

以上是关于HDU - 6616 Divide the Stones的主要内容,如果未能解决你的问题,请参考以下文章

贪心HDU 5783 Divide the Sequence

HDU 5783 Divide the Sequence

HDU4301 Divide Chocolate

HDU4301Divide Chocolate

HDU 4301 Divide Chocolate(DP)

UVA 10256 The Great Divide (凸包,多边形的位置关系)