C - Pashmak and Buses
来源:互联网 发布:优化旅客列车编组 编辑:程序博客网 时间:2024/05/19 02:43
C - Pashmak and Buses
Codeforces Round #261 (Div. 2)
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.
The first line of input contains three space-separated integers n, k, d (1 ≤ n, d ≤ 1000; 1 ≤ k ≤ 109).
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.
3 2 2
1 1 2
1 2 1
3 2 1
-1
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,n个人坐k辆车过d天,要求没有2个人是每天都在一辆车上的。若不可能,输出-1;否则输出每个人每天坐哪趟车。
题解:
一个人在全部d天中每天坐哪辆车,可以表示为d位k进制数x。那么2个人每天都在同一辆车上,就是两个人的x相等。所以我们只要构造出n个不同的d位k进制数就行。
先判若d^k<n则无解。(注意用(int)pow(5,4)会得比实际结果小1的整数……pow还是用double来撸比较好)
然后就撸出n个d位k进制数,我是直接0~n-1,转换成k进制数。
最后输出,我是由0~k-1组成的k进制数,输出时加1就行。
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
int n,k,d;
int a[1111][1111];
bool farm(){
int i;
if((double)n > (pow((double)k,d)))return 0;
memset(a,0,sizeof(a));
for(i=0;i<n;i++){
int x=i,j=0;
while(x){
a[i][j++]=x%k;
x/=k;
}
}
return 1;
}
int main(){
int i,j;
scanf("%d%d%d",&n,&k,&d);///人数、车数、天数
///搞n个不同的d位k进制数,作为n个学生d天坐的车
int ans=farm();
if(ans==0)puts("-1");
else{
for(i=0;i<d;i++){
printf("%d",a[0][i]+1);
for(j=1;j<n;j++){
printf(" %d",a[j][i]+1);
}
puts("");
}
}
return 0;
}
--------------------------------------------------------------
my code:
#include<stdio.h>
#include<string.h>
int a[1111][1111];
int n,k,d,t;
int main()
{
int i,j,ti;
while(scanf("%d%d%d",&n,&k,&d)!=EOF)
{
t=1;
for(i=1;i<=d;i++)
{
t*=k;
if(t>=n)
break;
}
if(i==d+1)
{
printf("-1\n");
continue;
}
memset(a,0,sizeof(a));
for(i=0;i<n;i++)
{
j=0;
ti=i;
while(ti!=0)
{
a[i][j++]=ti%k;
ti/=k;
}
}
for(i=d-1;i>=0;i--)
{
for(j=0;j<n;j++)
{
printf("%d ",a[j][i]+1);//矩阵转换
}
printf("\n");
}
}
return 0;
}
- C - Pashmak and Buses
- codeforces C. Pashmak and Buses
- 【CODEFORCES】 C. Pashmak and Buses
- Codeforces problem 459C Pashmak and Buses
- Codeforces 459C Pashmak and Buses(构造)
- CF 459C(Pashmak and Buses-分治)
- cf459C Pashmak and Buses
- codeforces #261 C题 Pashmak and Buses(瞎搞)
- Codeforces #261 (Div. 2) C. Pashmak and Buses(数学)
- Codeforces Round #261 (Div. 2) C. Pashmak and Buses
- Codeforces Round #261 (Div. 2) C. Pashmak and Buses
- codeforces 459C - Pashmak and Buses 【构造题】
- Codeforces Round #261 (Div. 2) C. Pashmak and Buses
- CodeForces 459C-G - Pashmak and Buses-构造-K进制
- Codeforces 459C Pashmak and Buses 可重集排列
- CF. Pashmak and Buses(分析)
- CF459C Pashmak and Buses 打印全排列
- Codeforces Round #261 (Div. 2)——Pashmak and Buses
- json解析
- java将String字符串写入文件
- 【设计分享】Altera FPGA及Quartues应用小实例
- magento - 获取购物车中的总额,运费,tax ,count,grandtotal等
- 修改DNS缓存TTL生存时间的生效期
- C - Pashmak and Buses
- 关于JFormattedTextField清空数据的方法
- 对数据结构中栈的一些理解
- linux powerpc i2c驱动 之 i2c设备层的注册过程
- Andorid安裝过程中的一个小事情
- MySQL基础1--如何改MySQL密码
- ril qmi qmuxd
- 【遇见Android bug】Can not perform this action after onSaveInstanceState
- asp.net JQuery Ajax WebService Gridview 任意 双击 某一列 编辑修改