【CODEFORCES】 C. Pashmak and Buses

2775 단어 수학.codeforces
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이 그보다 크면 해가 없고 그렇지 않으면 해가 있다.우리는 세로로 이 수조를 완성하고 가로로 출력하면 된다.
코드포렉스의 문제풀이를 보고 썼는데...
#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;
}

좋은 웹페이지 즐겨찾기