CodeForces 459 C.Pashmak and Buses(构造)
2018-01-09 11:05
471 查看
Description
有n个人,k辆车,在d天要去d个地方游玩,不允许存在两个人这d天都在同一辆车上,问是否存在合法方案
Input
三个整数n,k,d(1≤n,d≤1000,1≤k≤109)
Output
如果无解则输出−1,否则输出d行,每行n个整数表示这一天这n个人坐车的编号
Sample Input
3 2 2
Sample Output
1 1 2
1 2 1
Solution
一个人这d天乘坐的车辆编号形成一个长度为d,每个数字介于1~k之间的序列,只要每个人的这个序列是唯一的则必然有解,故只要n≤kd则有解,否则无解,对于有解的情况,把每个排列看作一个d位k进制数构造即可
Code
有n个人,k辆车,在d天要去d个地方游玩,不允许存在两个人这d天都在同一辆车上,问是否存在合法方案
Input
三个整数n,k,d(1≤n,d≤1000,1≤k≤109)
Output
如果无解则输出−1,否则输出d行,每行n个整数表示这一天这n个人坐车的编号
Sample Input
3 2 2
Sample Output
1 1 2
1 2 1
Solution
一个人这d天乘坐的车辆编号形成一个长度为d,每个数字介于1~k之间的序列,只要每个人的这个序列是唯一的则必然有解,故只要n≤kd则有解,否则无解,对于有解的情况,把每个排列看作一个d位k进制数构造即可
Code
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<ctime> using namespace std; typedef long long ll; typedef pair<int,int>P; const int INF=0x3f3f3f3f,maxn=1005; int n,k,d,a[maxn][maxn]; int main() { while(~scanf("%d%d%d",&n,&k,&d)) { ll temp=1; for(int i=1;i<=d;i++) { temp*=k; if(temp>=n)break; } if(temp<n) { printf("-1\n"); continue; } temp=1; for(int i=1;i<=d;i++) { for(int j=1,num=1;j<=n;j+=temp,num++) { if(num>k)num=1; for(int k=0;k<temp&&j+k<=n;k++) a[i][j+k]=num; } temp*=k; if(temp>n)temp=n; } for(int i=1;i<=d;i++) for(int j=1;j<=n;j++) printf("%d%c",a[i][j],j==n?'\n':' '); } return 0; }
相关文章推荐
- CodeForces 459C-G - Pashmak and Buses-构造-K进制
- codeforces 459C - Pashmak and Buses 【构造题】
- codeforces 459C - Pashmak and Buses 【构造题】
- codeforces 459 A. Pashmak and Garden 解题报告
- Pashmak and Buses(构造)
- CodeForces - 459C - Pashmak and Buses
- 【CODEFORCES】 C. Pashmak and Buses
- codeforces 459 B.Pashmak and Flowers 解题报告
- codeforces 536 c Tavas and Pashmaks
- CodeForces 323 A.Black-and-White Cube(构造)
- codeforces 449C C. Jzzhu and Apples(数论+构造)
- Codeforces 894C:Marco and GCD Sequence(构造)
- codeforces #261 C题 Pashmak and Buses(瞎搞)
- Codeforces Round #261 (Div. 2) C. Pashmak and Buses
- 【CODEFORCES】 D. Pashmak and Parmida's problem
- CF_459B_PashmakAndFlowers
- Codeforces Round #261 (Div. 2)——Pashmak and Buses
- codeforce Pashmak and Buses(dfs枚举)
- Codeforces Round #261 (Div. 2) C. Pashmak and Buses
- codeforces—— 459B —— Pashmak and Flowers