codeforces 459 C. Pashmak and Buses(思维)

题目链接:http://codeforces.com/problemset/problem/459/C

题意:有n个人,k辆车,d天要求没有两个人在d天都坐在一起。输出坐的方法。

 

题解:这题很有意思,首先在这d天里每个人都有一个序列那就是每天坐哪辆车。然后为了满足题意只要他们

的序列都是独一无二的就行。也就是说k辆车,d天总共有k^d次方的坐法,所以最多能提供k^d个人。然后

就是怎么输出了,这有一个技巧,只要构成d位的k进制数就行。然后遍历1~n依次加1即可。

 

#include <iostream>
#include <cstring>
using namespace std;
int a[2000][2000];
int main() {
    int n , k , d;
    cin >> n >> k >> d;
    int flag = 0;
    long long sum = 1;
    for(int i = 1 ; i <= d ; i++) {
        sum *= k;
        if(sum >= n) {
            flag = 1;
            break;
        }
    }
    if(flag) {
        for(int j = 1 ; j <= d ; j++) {
            a[1][j] = 1;
        }
        for(int i = 2 ; i <= n ; i++) {
            int tmp = 0;
            for(int j = d ; j >= 1 ; j--) {
                if(tmp) {
                    a[i][j] = a[i - 1][j];
                    continue;
                }
                if(a[i - 1][j] + 1 <= k) {
                    a[i][j] = a[i - 1][j] + 1;
                    tmp = 1;
                }
                else {
                    a[i][j] = 1;
                }
            }
        }
        for(int i = 1 ; i <= d ; i++) {
            for(int j = 1 ; j <= n ; j++) {
                cout << a[j][i] << ' ';
            }
            cout << endl;
        }
    }
    else {
        cout << -1 << endl;
    }
    return 0;
}
posted @ 2017-04-27 16:10  Gealo  阅读(241)  评论(0编辑  收藏  举报