CodeForces 459C Pashmak and Buses(构造)题解
题意:n个人,k辆车,要求d天内任意两人都不能一直在同一辆车,能做到给出构造,不能输出-1
思路:我们把某一个人这d天的车号看成一个d位的数字,比如 1 1 2 3代表第一天1号车、第二天1号车、第三天2号车、第四天3号车,那么就是求构造n个不相同的d位数,所以只要kd >= n,然后模拟加一
代码:
#include<set> #include<map> #include<stack> #include<cmath> #include<queue> #include<vector> #include<string> #include<cstdio> #include<cstring> #include<sstream> #include<iostream> #include<algorithm> typedef long long ll; using namespace std; const int maxn = 1e3 + 10; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; bool check(ll a, ll b, ll n){ ll ret = 1; while(b){ if(b & 1) ret = ret * a; a = a * a; b >>= 1; if(ret >= n) return true; } return false; } int ans[maxn][maxn]; int main(){ int n, k, d; scanf("%d%d%d", &n, &k, &d); if(check(min(k, n), d, n)){ for(int i = 1; i <= d; i++) ans[i][1] = 1; for(int i = 2; i <= n; i++){ int c = 0; //进位 ans[1][i] = ans[1][i - 1] + 1; if(ans[1][i] > k){ ans[1][i] = 1; c = 1; } for(int j = 2; j <= d; j++){ ans[j][i] = ans[j][i - 1] + c; c = 0; if(ans[j][i] > k){ ans[j][i] = 1; c = 1; } } } for(int i = 1; i <= d; i++){ for(int j = 1; j <= n; j++){ if(j != 1) printf(" "); printf("%d", ans[i][j]); } printf("\n"); } } else printf("-1\n"); return 0; }