CF 459C(Pashmak and Buses-分治)

C. Pashmak and Buses
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently Pashmak has been employed in a transportation company. The company has k buses and has a contract with a school which has n students. The school planned to take the students to d different places for d days (each day in one place). Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all d days.

Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity.

Input

The first line of input contains three space-separated integers n, k, d (1 ≤ n, d ≤ 1000; 1 ≤ k ≤ 109).

Output

If there is no valid arrangement just print -1. Otherwise print d lines, in each of them print n integers. The j-th integer of the i-th line shows which bus the j-th student has to take on the i-th day. You can assume that the buses are numbered from 1 to k.

Sample test(s)
input
3 2 2
output
1 1 2 
1 2 1 
input
3 2 1
output
-1
Note

Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day.


要让任意2人至少有1天不做同1bus

第一天分:111222333

第二天分:123123123


注意到第1天将人分为k份,同份中人两两认识,不同份中的人一定不认识

因此只需要单独考虑每份,让每份中的人不认识就够了


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,k,d;
int a[MAXN][MAXN];
int dfs(int l,int r,int s)
{
		int t=r-l+1;
		int p1=t/k,p2=t%k;
		int blo=min(t,k);
		int j=l;
		For(i,blo)
		{
			int len=p1;if (p2) len++,p2--;
			Fork(k,j,j+len-1) a[s][k]=i;
			if (s<d) dfs(j,j+len-1,s+1);
			j+=len;
		}
}
int main()
{
//	freopen("Buses.in","r",stdin);
//	freopen(".out","w",stdout);
	cin>>n>>k>>d;
	
	int t=n;
	For(i,d)
		t=t/k+(bool)(t%k>0);
	if (t==1) 
	{
		dfs(1,n,1);
		For(i,d)
		{
			For(j,n-1) printf("%d ",a[i][j]);
			printf("%d\n",a[i][n]);
		}
	}
	else cout<<"-1"<<endl;
	
	
	return 0;
}







你可能感兴趣的:(CF 459C(Pashmak and Buses-分治))