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. ...
随机推荐
- 【codevs1014/1068】背包型动态规划
分析: 状态转移方程: v[j]=max(v[j],v[j-a[i]]+a[i]) (j ← tol downto a[i]) /* 作者:flipped 题目:p1014 装箱问题 */ #incl ...
- 查询条件Where
1.字符串 $condition = 'name=\'Lily\' and age>10'; 2.数组 ['type' => 1, 'status' => 1] //生成 (type ...
- list 内部方法
代码 #list内部方法 l=['a','9','c','a','3','7'] print(dir(l)) l.append('v') print(l)#append(self, p_object) ...
- 细菌觅食算法-python实现
BFOIndividual.py import numpy as np import ObjFunction class BFOIndividual: ''' individual of bateri ...
- udp内网穿透 两个内网互联
1,在有外网ip的机器上启动server. package udp; import java.net.DatagramPacket; import java.net.InetSocketAddress ...
- Graphtree--zabbix增强功能(一屏展示所有内容)
Graphtree--zabbix增强功能 Graphtree由OneOaaS开发并开源出来. 功能 集中展示所有分组设备 集中展示一个分组图像 集中展示一个设备图像 展示设备下的Applicatio ...
- IE input file隐藏不能上传文件解决方法
当大神们都在探讨更深层次的问题时,我还在这里转载发些肤浅的问题解决方案.罢了,为了和我一样笨的后来人. 问题: 上传文件时,用<input type="file" /> ...
- 重温设计模式(三)——职责链模式(chain of responsibility)
一. 写在前面的 这么多的设计模式,我觉得职责链是我第一次看上去最简单,可是回想起来却又最复杂的一个模式. 因此,这个文章我酝酿了很久,一直也没有胆量发出来,例子也是改了又改,可是仍然觉得不够合理.所 ...
- jar包与lib包的区别
jar包是编译时使用,假如编译出错代码没问题一定是jar包的问题,lib是运行时使用,比如程序启动后出错了但是编译没有问题,就可能是lib出错了,不会是jar包的问题.
- ORA-00931: missing identifier ORA-06512: at "SYS.DBMS_UTILITY"
Database db = DatabaseFactory.CreateDatabase(); string sql = "SELECT * FROM table&qu ...