CF459C Pashmak and Buses (构造d位k进制数
Codeforces Round #261 (Div. 2)
C. Pashmak and Buses
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputRecently 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 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,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就行。
1 //#pragma comment(linker, "/STACK:102400000,102400000") 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 #include<cstring> 6 #include<algorithm> 7 #include<cmath> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<queue> 12 using namespace std; 13 #define ll long long 14 #define usll unsigned ll 15 #define mz(array) memset(array, 0, sizeof(array)) 16 #define minf(array) memset(array, 0x3f, sizeof(array)) 17 #define REP(i,n) for(i=0;i<(n);i++) 18 #define FOR(i,x,n) for(i=(x);i<=(n);i++) 19 #define RD(x) scanf("%d",&x) 20 #define RD2(x,y) scanf("%d%d",&x,&y) 21 #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z) 22 #define WN(x) prllf("%d\n",x); 23 #define RE freopen("D.in","r",stdin) 24 #define WE freopen("1biao.out","w",stdout) 25 #define mp make_pair 26 int n,k,d; 27 int a[1111][1111]; 28 bool farm(){ 29 int i; 30 if((double)n > (pow((double)k,d)))return 0; 31 memset(a,0,sizeof(a)); 32 for(i=0;i<n;i++){ 33 int x=i,j=0; 34 while(x){ 35 a[i][j++]=x%k; 36 x/=k; 37 } 38 } 39 return 1; 40 } 41 42 int main(){ 43 int i,j; 44 scanf("%d%d%d",&n,&k,&d);///人数、车数、天数 45 ///搞n个不同的d位k进制数,作为n个学生d天坐的车 46 int ans=farm(); 47 if(ans==0)puts("-1"); 48 else{ 49 for(i=0;i<d;i++){ 50 printf("%d",a[0][i]+1); 51 for(j=1;j<n;j++){ 52 printf(" %d",a[j][i]+1); 53 } 54 puts(""); 55 } 56 57 } 58 return 0; 59 }