Codeforces 459C Pashmak and Buses(构造)

题目链接:Codeforces 459C Pashmak and Buses

题目大意:有n个学生,d天,k辆车,每天会到一个不同的地方,即要乘坐一次车,要求每人两个学生在每一天共同乘坐一辆车。个数安排方案。

解题思路:每个学生的乘坐方案即使一个长度为d,元素范围1~k的序列,不出现重复即可。dfs处理出方案,方案数足够后跳出递归。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 1005;

ll d, k;
int n, c, v[maxn][maxn], arr[maxn];

bool judge () {
    ll ret = 1;
    for (int i = 1; i <= d; i++) {
        ret *= k;
        if (ret >= n)
            return true;
    }
    return false;
}

void solve (int u) {

    if (d == u) {
        memcpy(v[c++], arr, sizeof(arr));
        return;
    }

    int& i = arr[u];
    for (i = 1; i <= k; i++) {
        solve(u+1);
        if (c == n)
            return;
    }
}

int main () {
    scanf("%d%lld%lld", &n, &k, &d);

    if (judge ()) {
        c = 0;
        solve(0);
        for (int i = 0; i < d; i++) {
            printf("%d", v[0][i]);
            for (int j = 1; j < n; j++)
                printf(" %d", v[j][i]);
            printf("\n");
        }
    } else
        printf("-1\n");
    return 0;
}

你可能感兴趣的:(Codeforces 459C Pashmak and Buses(构造))