您的位置:首页 > 其它

CodeForces 459 C.Pashmak and Buses(构造)

2018-01-09 11:05 471 查看
Description

有n个人,k辆车,在d天要去d个地方游玩,不允许存在两个人这d天都在同一辆车上,问是否存在合法方案

Input

三个整数n,k,d(1≤n,d≤1000,1≤k≤109)

Output

如果无解则输出−1,否则输出d行,每行n个整数表示这一天这n个人坐车的编号

Sample Input

3 2 2

Sample Output

1 1 2

1 2 1

Solution

一个人这d天乘坐的车辆编号形成一个长度为d,每个数字介于1~k之间的序列,只要每个人的这个序列是唯一的则必然有解,故只要n≤kd则有解,否则无解,对于有解的情况,把每个排列看作一个d位k进制数构造即可

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=1005;
int n,k,d,a[maxn][maxn];
int main()
{
while(~scanf("%d%d%d",&n,&k,&d))
{
ll temp=1;
for(int i=1;i<=d;i++)
{
temp*=k;
if(temp>=n)break;
}
if(temp<n)
{
printf("-1\n");
continue;
}
temp=1;
for(int i=1;i<=d;i++)
{
for(int j=1,num=1;j<=n;j+=temp,num++)
{
if(num>k)num=1;
for(int k=0;k<temp&&j+k<=n;k++)
a[i][j+k]=num;
}
temp*=k;
if(temp>n)temp=n;
}
for(int i=1;i<=d;i++)
for(int j=1;j<=n;j++)
printf("%d%c",a[i][j],j==n?'\n':' ');
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: