CF459C Pashmak and Buses 列印全排列,cf459cpashmak

來源:互聯網
上載者:User

CF459C Pashmak and Buses 列印全排列,cf459cpashmak

        這題如果將最終的結果豎著看,每一列構成的數可以看成是k進位的數,一共有d列,任意兩列都不相同,所以這就是一個d位k進位數全排列的問題,一共有k ^ d個排列,如果k ^ d < n,則列印-1。

        列印最終結果時設第一列就為1 1 1 1 ... 1,然後依次每列增加1後(公交車編號從1開始,不是從0開始) ,注意,這裡是k進位。

#include <stdlib.h>  #include <stdio.h>  #include <algorithm>  #include <vector>  #include <math.h>using namespace std;int tmpvalue[1001][1001];int main(){#ifdef _DEBUGfreopen("d:\\in.txt", "r", stdin);#endifint n, k, d;scanf("%d %d %d", &n, &k, &d);if (k < n){int total = 1;for (int i = 1; i <= d;i++){total *= k;if (total >= n){break;}}if (total < n){printf("-1\n");return 0;}}for (int i = 1; i <= d;i++){tmpvalue[i][1] = 1;}for (int i = 2; i <= n;i++){for (int j = 1; j <= d;j++){tmpvalue[j][i] = tmpvalue[j][i - 1];}tmpvalue[1][i] += 1;for (int j = 1; j <= d; j++){if (tmpvalue[j][i] > k){tmpvalue[j][i] = 1;tmpvalue[j + 1][i]++;}elsebreak;}}for (int i = 1; i <= d;i++){for (int j = 1; j <= n;j++){printf("%d ", tmpvalue[i][j]);}printf("\n");}return 0;}


        




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.