2019杭电多校第四场1003题解

2019杭电多校第四场1003题解
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6616

AC代码:

#include
#define endl '\n'
#define css(n) cout<
#define p_queue priority_queue 
#define sd(a) scanf("%d",&a)
#define sld(a) scanf("%lld",&a)
#define m(a,b) memset(a,b,sizeof a)
#define pb push_back 
#define lson id<<1
#define rson id<<1|1
typedef long long ll;
using namespace std;
const int maxn = 100000+50,modd = 1e9 + 7,inf = 0x3f3f3f3f,INF = 0x7fffffff;
inline ll min(ll a,ll b){return a < b ? a : b;}
inline ll max(ll a,ll b){return a > b ? a : b;}
inline ll gcd(ll a,ll b){ return b==0? a: gcd(b,a%b); }
inline ll exgcd(ll a,ll b,ll &x,ll &y){ ll d; (b==0? (x=1,y=0,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d; }
inline ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%modd;a=a*a%modd;n>>=1;}return sum;}
inline ll qmul(ll a,ll n){ll sum=0;while(n){if(n&1)sum=(sum+a)%modd;a=(a+a)%modd;n>>=1;}return sum;}
inline ll inv(ll a) {return qpow(a,modd-2);}//求逆元 
inline ll madd(ll a,ll b){return (a%modd+b%modd)%modd;}//加后取模 
inline ll mmul(ll a,ll b){return a%modd * b%modd;}//乘后取模 
int n,m,t,k;
int arr[maxn];
ll sum;
vector<int> ans[maxn];
int main()
{
	sd(t);
	while(t--)
	{
		sd(n);sd(k);
		
		 if(k==1)
		 {
		 	printf("yes\n");
		 	for(int i=1;i<=n;i++)
		 	{
		 		if(i==1) printf("%d",i);else printf(" %d",i);
			 }
			 printf("\n");
			 continue;
		 }
		 if(k==n)
		{
			printf("no\n");continue;
		 } 
		for(int i=1;i<=n;i++)
		{
			arr[i]=i;
		}
		sum=1LL*(n+1)*1LL*n/(2*1LL);
//		cout<
		if(sum%k!=0)
		{
			printf("no\n");continue;
		}
		for(int i=1;i<=k;i++) ans[i].clear();
		int sum1=sum/k; //每组的重量 
		int flag=n/k; //每组多少个 
		if(flag%2==0)
		{
			int tot=1;
					for(int i=1;i<=k;i++)
					{
						for(int zz=1;zz<=flag/2;zz++)
						{
							ans[i].push_back(tot);
					        ans[i].push_back(n-tot+1);
					        tot++;
						}
					}
			printf("yes\n");
			for(int i=1;i<=k;i++)
			{
				int siz=ans[i].size();
				for(int j=0;j<siz;j++)
				{
					if(j==0) printf("%d",ans[i][j]);
					else printf(" %d",ans[i][j]);
				}
				printf("\n");
			} 
			continue;
		}
		if(flag%2==1)
		{
			int a,b,c;
			a=1;b=(1+k)/2;c=k; 
			for(int i=1;i<=k;i++)
			{
				ans[i].push_back(a);
				ans[i].push_back(b+k);
				ans[i].push_back(c+2*k);
				a=(a+1)%(k+1);
				b=(b+1)%(k+1);
				c=(c-2+k)%k;
				if(!a) a=1;if(!b) b=1;if(!c) c=1;
			}
			int tot=3*k+1;
			for(int i=1;i<=k;i++)
			{
				for(int j=1;j<=(flag-3)/2;j++)
				{
					ans[i].push_back(tot);
					ans[i].push_back(n+3*k+1-tot);
					tot++;
				}
			}
			printf("yes\n");
		for(int i=1;i<=k;i++)
		{
			int siz=ans[i].size();
			for(int j=0;j<siz;j++)
			{
				if(j==0) printf("%d",ans[i][j]);
				else printf(" %d",ans[i][j]);
			}
			printf("\n");	
		}
	 } 
}
	return 0;
} 

你可能感兴趣的:(思维)