codeforces 459C Pashmak and Buses 解题报告
题目链接:http://codeforces.com/problemset/problem/459/C
题目意思:有 n 个 students,k 辆 buses。问是否能对 n 个students安排每一天搭乘的buses,使得没有两个student 在 d 天搭乘相同的buses,buses 的 容量没有限制,也就是它可以搭无限多的人。
做这条题的时候,我是得不出无解的条件的,看了 tutorial 之后一下子明白了,就是 k^d > n。很容易理解,因为每一天某个student可以选择的 buses 都有 k 种选择嘛~~~而且又因为要输出 d 行 n 列 那么多的数,所以 k ^ d > n 表示至少有两列数会使得某两个student 成为 close friend!
知道这个之后就是如何构造答案了,其实就是所有排列情况,不过我是不会做啦~~~看了作者的代码自己重新写,非常惊叹他的智慧呀~~~
拿这组数据来说吧,20 3 5
结合代码中注释为transfer
ans[i][j] = ans[i-1][j]; 表示从左边那一列复制到下一列,那么为了不使得两个student成为close friend 就势必要使得他们有一点不同,这时就用到代码中的 update 操作了
ans[i][j] = (ans[i][j] + 1) % k;
这行代码用得相当巧妙,由于循环是从 d-1 ——> 0 的,也就是对于当前处理的那一列,从下往上更改(如果遇到 % k == 0 的情况),否则(% k != 0)就在上一列的对应行 + 1。
最后还有一个地方,在判断 k^d > n 的时候,如果可以提早发现有answer,要提早 break 出来,从 test7 可以知道!考虑数据范围:1 ≤ n, d ≤ 1000; 1 ≤ k ≤ 10^9 ,因为乘的时候有可能很大,超出long long 范围 !!
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; typedef long long LL;
const int maxn = + ; int ans[maxn][maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif int n, k, d;
while (scanf("%d%d%d", &n, &k, &d) != EOF)
{
bool flag = false;
LL product = ;
for (int i = ; i < d; i++)
{
product *= (LL)k; // one select has k choice
if (product >= n) // mean has answer
{
flag = true;
break;
}
} if (!flag) // repeat, no answer
printf("-1\n");
else // construct the answer
{
memset(ans, , sizeof(ans));
for (int i = ; i < n; i++)
{
for (int j = ; j < d; j++)
{
ans[i][j] = ans[i-][j]; // right transfer
} for (int j = d-; j >= ; j--) // update
{
ans[i][j] = (ans[i][j] + ) % k;
if (ans[i][j])
break;
}
}
for (int i = ; i < d; i++)
{
for (int j = ; j < n; j++)
printf("%d ", ans[j][i]+);
printf("\n");
}
}
}
return ;
}
codeforces 459C Pashmak and Buses 解题报告的更多相关文章
- CodeForces - 459C - Pashmak and Buses
先上题目+: C. Pashmak and Buses time limit per test 1 second memory limit per test 256 megabytes input s ...
- codeforces 459C Pashmak and Buses(模拟,组合数A)
题目 跑个案例看看结果就知道了:8 2 3 题目给的数据是 n,k,d 相当于高中数学题:k个人中选择d个人排成一列,有多少种不同的方案数,列出其中n中就可以了. #include<iostre ...
- CodeForces 459C Pashmak and Buses(构造)题解
题意:n个人,k辆车,要求d天内任意两人都不能一直在同一辆车,能做到给出构造,不能输出-1 思路:我们把某一个人这d天的车号看成一个d位的数字,比如 1 1 2 3代表第一天1号车.第二天1号车.第三 ...
- Codeforces 459C Pashmak and Buses 机智数学题
这个题目说的是有n个人,有k辆巴士,有m天,每天都要安排n个人坐巴士(可以有巴士为空),为了使得这n个人不会成为朋友,只要每两个人在这m天里坐的巴士至少一天不相同即可. 要你求是否有这样的安排方法,如 ...
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- cf 459c Pashmak and Buses
E - Pashmak and Buses Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- codeforces 459 A. Pashmak and Garden 解题报告
题目链接:http://codeforces.com/problemset/problem/459/A 题目意思:给出两个点的坐标你,问能否判断是一个正方形,能则输出剩下两点的坐标,不能就输出 -1. ...
随机推荐
- oracle存储过程执行中输出日志文件
create or replace procedure p_outputdebug(a varchar2,b varchar2,c varchar2)is vFileName varchar2(100 ...
- 求第N数大问题
问题: InputThe first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of ...
- IIS FTP Server Anonymous Writeable Reinforcement, WEBDAV Anonymous Writeable Reinforcement(undone)
目录 . 引言 . IIS 6.0 FTP匿名登录.匿名可写加固 . IIS 7.0 FTP匿名登录.匿名可写加固 . IIS >= 7.5 FTP匿名登录.匿名可写加固 . IIS 6.0 A ...
- 修改eclipse/MyEclipse中包的显示结构为树形
在右上边三角那里进去设置 选第一个是显示完整的包名,第二个显示的是树形结构,我们一般用第一种,如下图:
- ci创建zip
public function createZip() { $this->load->library("zip"); $name = "test.text&q ...
- linux4
linux 特点:1.免费 开源(代码公开)2.支持多线程/多用户的操作系统3.安全性4.对内存和文件管理有自己的一套优越的方法 linux最小只需要4M ->嵌入式开发默认不启动用户界面roo ...
- nginx负载均衡的配置方法
upstream www.***.com { server ; server ; server ; } server { listen ; server_name www.***.com; #char ...
- linux vsftpd搭建
1.yum install vsftpd; 2.关闭SELinux的方法:修改/etc/selinux/config文件中的SELINUX="" 为 disabled ,然后重启 ...
- [Angularjs]ng-file-upload上传文件
写在前面 最近在弄文档库的H5版,就查找了下相关的上传组件,发现了ng-upload的东东,推荐给大家. 系列文章 [Angularjs]ng-select和ng-options [Angularjs ...
- Ubuntu 中软件的安装、卸载以及查看的方法总结
Ubuntu 中软件的安装.卸载以及查看的方法总结 博客分类: Linux UbuntuDebian配置管理CacheF# 说明:由于图形化界面方法(如Add/Remove... 和Synaptic ...