CodeForces 459C-Pashmak and Buses(思维题)

CodeForces 459C-Pashmak and Buses(思维题)

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.

Examples
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.

题目大意:

有n个人 k辆车,d天,不能有两个人k天都是坐得同一辆车。如果做不到输出-1,如果做得到就输出每天每个人坐得车,也就是d行,每行n个数,每个数在1-k之间。

解题思路:

我们换一种思考角度,考虑每个人,在d天里坐得车得编号分别事多少。那每个人都有d天,每天坐得车是1-k之间得任何一个数,也就是一共有 d个k相乘种坐法也就是kd ,如果n<kd 那就一定可以。
换句话说就是有n个 d位k进制数。
我们用个二维数组a[i][j],表示第i个人第j天坐得哪辆车,那么输出得时候只要按列输出就行,一列一列得输出,不就相当于一天一天得输出嘛
有几个细节需要注意一下:
1.判断时候存在的时候不要直接求 kd^ 因为k是1e9最大,你这样求肯定超精度,你就一层一层得乘,看是否大于n,因为n最大1000,所以耗时也基本可以忽略

	int flag=k;
        for(int i=0;i<d-1;i++)
            {
                if(flag>n) break;
                flag*=k;

            }

2.n=1得时候需要特判一下,n等于1,无论k d等于啥,只要输入d天得1就好啦.
3.输出得时候一定要按列输出,别按行,行我们记录是每个人这k天坐得车,而列才是代表每一天得。

for(int j=1;j<=d;j++)
		{
			for(int i=1;i<=n;i++)
			{
				cout<<a[i][j]+1;
				if(i!=n)
					cout<<" ";
				else
					cout<<endl;
			}

4.因为我们车得编号从1开始到k,但是a初始化都为0,所以输出得时候统一+1。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define ll long long
using namespace std;
const int maxn=2e3+10;
int a[maxn][maxn];
ll n,k,d;
int main()
{
	////////
	while(cin>>n>>k>>d)
	{
		if(n==1)//////特判一下 
		{
			for(int i=0;i<d;i++)
				cout<<"1"<<endl;
			continue;
		}
		memset(a,0,sizeof(a));
		int flag=k;
        for(int i=0;i<d-1;i++)
            {
                if(flag>n) break;
                flag*=k;

            }
        if(flag<n)
        {
            printf("-1\n");
            continue;
        }
		for(int i=1;i<=n;i++)
		{
			int tmp=i;
			int cnt=1;
			while(tmp)
			{
				a[i][cnt++]=tmp%k;
				tmp/=k;
			}
		}
		for(int j=1;j<=d;j++)
		{
			for(int i=1;i<=n;i++)
			{
				cout<<a[i][j]+1;
				if(i!=n)
					cout<<" ";
				else
					cout<<endl;
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43179892/article/details/84933231