标签:影响 == ref .text roc 数据 ace scanf code
给出一个蛋糕。切成1~n大小的n块。问是否能在不继续分割的情况下拼凑出m等份。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
int t;
LL n,m;
vector<int> v[20];
int mark[100007];
int main ( )
{
scanf ( "%d" , &t );
while ( t-- )
{
memset ( mark , 0 , sizeof ( mark ));
for ( int i = 0 ; i < 20 ; i++ )
v[i].clear();
scanf ( "%I64d%I64d" , &n , &m );
LL ans = n*(n+1)/2;
LL num = ans/m;
if ( ans%m || num < n )
{
puts ( "NO" );
continue;
}
LL r = n;
int i;
for ( i = 0 ; i < m ; i++ )
{
//cout <<n << " " << num << " "<< i << " " << r << endl;
LL temp = num;
bool flag = true;
while ( mark[r] && r > 1 ) r--;
while ( temp >= r && !mark[r] )
{
v[i].push_back ( r );
mark[r] = true;
temp -= r;
r--;
if ( r == 0 ) break;
while ( mark[r] && r > 1 ) r--;
}
if ( temp == 0 )
{
flag = false;
}
else if ( !mark[temp] )
{
v[i].push_back ( temp );
mark[temp] = true;
flag = false;
}
else
{
while ( true )
{
bool finish = false;
for ( int j = temp ; j > 0 ; j-- )
{
if ( !mark[j])
{
mark[j] = true;
v[i].push_back ( j );
temp -= j;
finish = true;
break;
}
}
if ( !finish ) break;
if ( temp == 0 )
{
flag = false;
break;
}
}
}
if ( flag ) break;
}
if ( i == m )
{
puts ( "YES" );
for ( int i = 0 ; i < m ; i++ )
{
printf ( "%d" , v[i].size() );
for ( int j = 0 ; j < v[i].size() ; j++ )
printf ( " %d" , v[i][j] );
puts ( "");
}
}
else puts ( "NO" );
}
}
标签:影响 == ref .text roc 数据 ace scanf code
原文地址:http://www.cnblogs.com/mfmdaoyou/p/6859931.html