4 1 2 5 3 5 2 9 3
NO YES 1 5 2 1 4 2 2 3 NO YES 3 1 5 9 3 2 6 7 3 3 4 8
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
#define LL long long
using namespace std;
const LL MAXN = 500000 + 10;
LL n, m;
LL ans[20][MAXN];
LL A[MAXN];
LL pos[MAXN];
LL tot, tar;
bool dfs(LL dep, LL now, LL u, LL c)
{
if(now == 0)
{
LL k = 0;
while(pos[k] != -1) k++;
pos[k] = c;
if(dfs(dep + 1, A[k], k + 1, c)) return true;
pos[k] = -1;
return false;
}
if(now == tar)
{
if(dep == tot) return true;
if(dfs(dep, 0, 0, c + 1)) return true;
}
for(LL i=u;i<tot;i++)
{
if(pos[i] == -1 && now + A[i] <= tar)
{
pos[i] = c;
if(dfs(dep + 1, now + A[i], i + 1, c)) return true;
pos[i] = -1;
}
}
return false;
}
int main()
{
LL T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(LL i=0;i<m;i++) for(LL j=0;j<n;j++) ans[i][j] = 0;
if((n * (n + 1) / 2) % m != 0 || (2 * m - 1) > n)
{
printf("NO\n");
continue;
}
while(n >= 40)
{
for(LL i=0;i<m;i++) ans[i][++ans[i][0]] = n - i;
for(LL i=0;i<m;i++) ans[i][++ans[i][0]] = n - 2 * m + 1 + i;
n -= 2 * m;
}
tot = n;
tar = n * (n + 1) / (2 * m);
for(LL i=0;i<tot;i++) A[i] = tot - i;
for(LL i=0;i<tot;i++) pos[i] = -1;
dfs(0, 0, 0, 0);
for(LL i=0;i<tot;i++) ans[pos[i]][++ans[pos[i]][0]] = A[i];
printf("YES\n");
for(LL i=0;i<m;i++)
{
printf("%d", ans[i][0]);
for(LL j=1;j<=ans[i][0];j++) printf(" %d", ans[i][j]);
printf("\n");
}
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)
原文:http://blog.csdn.net/moguxiaozhe/article/details/47335465