CodeForces - 459C - Pashmak and Buses

先上題目+:ios

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.c++

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

Input

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

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

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

 

  題意:有n我的,k輛車,d個景點,如今要求每一個學生天天去一個景點(一共d天)每選一個景點去的時候須要坐某一輛車,現要求這d天裏面,沒有任意兩我的是都坐在同一輛車裏面(不能d天都都在一塊兒)。blog

  作法:一開始我想的是從k輛車裏面選d輛,而後將n個學生分配進去,可是這樣作好像不行。後來就換了個思路,從k輛車裏面選d輛車(可重複)做爲某一個學生這d天的乘車方案,而後將這些排列的其中n個求出來就能夠了。固然,若是沒有這麼多的話就輸出-1。three

 

上代碼:ip

 

 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*/
相關文章
相關標籤/搜索