【CODEFORCES】 C. Pashmak and Buses

来源:互联网 发布:linux安装oracle乱码 编辑:程序博客网 时间:2024/05/25 21:34
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.


题解:这一题的解法很巧妙,首先在纸上画一个D行,N列的数组。行表示天,列表示学生,格子里面填车次,这样我们可以看到,每个学生在D天里每一天乘坐的车次可以构成一个序列。而这些序列一共有 d^k 个,若N比其大那么便无解,否则有解。我们只需要竖着完成这个数组然后在横着输出就可以了。

看了CODEFORECS上的题解才写出来………………

#include<iostream>#include<cmath>using namespace std;long long  a[1010][1010],n,k,d,t,i,j,p,flag;void solve(int n,int k,int d){    for (int i=1;i<=d;i++) a[i][1]=1;    i=2;    while (i<=n)    {        j=d; t=1;        while (j>=1)        {            a[j][i]=a[j][i-1]+t;            if (a[j][i]>k)            {                t=1;                a[j][i]=1;            }            else t=0;            j--;        }        i++;    }    for (int i=1;i<=d;i++)    {        for (int j=1;j<=n;j++)  cout<<a[i][j]<<" ";        cout <<endl;    }}int main(){    cin >>n>>k>>d;    p=1;    i=1; flag=0;    while (i<=d)    {        p*=k;        i++;        if (n<=p)        {            flag=1;            break;        }    }    if (flag) solve(n,k,d);    else  cout <<-1;    return 0;}



0 0
原创粉丝点击