codeforces C. Pashmak and Buses

来源:互联网 发布:linux挂载新硬盘 编辑:程序博客网 时间:2024/05/19 06:51

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.


题意:就是判断k^d和n的大小。关键是输出n种不同的排列,在这转化为K进制数,输出前N个数。


#include"stdio.h"#include"string.h"#include"iostream"#include"queue"#include"algorithm"using namespace std;#define N 1005#define LL __int64int ans[N][N];int main(){    int i,j,k,d,n;    while(scanf("%d%d%d",&n,&k,&d)!=-1)    {        LL tmp=1;        for(i=0;i<d;i++)        {            tmp*=k;            if(tmp>=n) break;        }        if(i==d)        {            printf("-1\n");            continue;        }        for(i=1;i<n;i++)        {            for(j=0;j<d;j++)     //ans[i]行记录的相等于一个数字,            {                ans[i][j]=ans[i-1][j];   //在ans[i-1]这个数字基础上加一得另一个数字(排列)            }            for(j=d-1;j>=0;j--)            {                ans[i][j]=(ans[i][j]+1)%k;    //从低位开始加一,                if(ans[i][j])           //若满K则进一                    break;             //若加一后则跳出循环,代表已找到一个排序            }        }        for(i=0;i<d;i++)        {            for(j=0;j<n;j++)            {                printf("%d ",ans[j][i]+1);            }            puts("");        }    }    return 0;}




0 1
原创粉丝点击