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

 

  题意:有n个人,k辆车,d个景点,现在要求每个学生每天去一个景点(一共d天)每选一个景点去的时候需要坐某一辆车,现要求这d天里面,没有任意两个人是都坐在同一辆车里面(不能d天都都在一起)。

  做法:一开始我想的是从k辆车里面选d辆,然后将n个学生分配进去,但是这样做好像不行。后来就换了个思路,从k辆车里面选d辆车(可重复)作为某一个学生这d天的乘车方案,然后将这些排列的其中n个求出来就可以了。当然,如果没有这么多的话就输出-1。

 

上代码:

 

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define MAX 1002
 4 using namespace std;
 5 
 6 ll n,k,d;
 7 ll s[MAX][MAX];
 8 ll arr[MAX],now;
 9 
10 bool check(){
11     ll ans=1;
12     for(ll w=d;w>0;w--){
13         ans=ans*k;
14         if(ans>=n) return 1;
15     }
16     return 0;
17 }
18 
19 bool cons(int tot){
20     if(tot==d){
21         for(int i=0;i<tot;i++){
22             s[i][now]=arr[i];
23         }
24         now++;
25         if(now==n) return 1;
26         return 0;
27     }
28     for(int i=1;i<=k;i++){
29         arr[tot]=i;
30         if(cons(tot+1)) return 1;
31     }
32     return 0;
33 }
34 
35 void print(){
36     for(int i=0;i<d;i++){
37         for(int j=0;j<n;j++){
38             if(j) cout<<" ";
39             cout<<s[i][j];
40         }
41         cout<<endl;
42     }
43 }
44 
45 int main()
46 {
47     //freopen("data.txt","r",stdin);
48     ios::sync_with_stdio(false);
49     while(cin>>n>>k>>d){
50         if(check()){
51             memset(s,0,sizeof(s));
52             now=0;
53             cons(0);
54             print();
55         }else{
56             cout<<"-1"<<endl;
57         }
58     }
59     return 0;
60 }
/*459C*/

 

posted @ 2014-09-12 11:24  海拉鲁的林克  阅读(297)  评论(0编辑  收藏  举报