题意:给出一个n行的棋盘,每行的长度任意,问在该棋盘中放k个车(不能同行或者同列)有多少种放法(n <= 250, 每行的长度 <= 250)。

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=269

——>>开始的时候冒险用dfs去做,结果TLE了。。。改dp,大数长度开小点WA,开大点MLE……最后改用滚动数组开1000位的大数长度才A掉……

设d[i][j]表示前i行放j个车的方法数,

则状态转移方程为:d[i][j] = d[i-1][j] + d[i-1][j-1] * (b[i] - j + 1);

改滚动数组:d[j] = d[j] + d[j-1] * (b[i] - j + 1);

#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm> using namespace std; const int maxn = 250 + 2;
const int maxl = 1000;
int b[maxn]; struct bign{
int len, s[maxl];
bign(){
memset(s, 0, sizeof(s));
len = 1;
}
bign operator = (int num){
len = 0;
while(num > 0){
s[len++] = num % 10;
num /= 10;
}
if(!len){
s[0] = 0;
len = 1;
}
return *this;
}
bign(int num){
*this = num;
}
bign operator + (const bign& b) const{
bign c;
c.len = 0;
for(int i = 0, g = 0; g || i < max(len, b.len); i++){
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % 10;
g = x / 10;
}
return c;
}
bign operator * (const bign& b) const
{
bign c;
c.len = len + b.len;
for(int i = 0; i < len; i++)
for(int j = 0; j < b.len; j++)
c.s[i+j] = c.s[i+j] + s[i] * b.s[j];
for(int i = 0; i < c.len-1; i++)
{
c.s[i+1] = c.s[i+1] + c.s[i] / 10;
c.s[i] = c.s[i] % 10;
}
while(c.len > 1 && !c.s[c.len-1]) c.len--;
return c;
}
void print(){
for(int i = len - 1; i >= 0; i--) printf("%d", s[i]);
printf("\n");
}
}d[maxn]; int main()
{
int n, k, i, j;
while(scanf("%d%d", &n, &k) == 2){
for(i = 1; i <= n; i++) scanf("%d", &b[i]);
sort(b+1, b+1+n);
memset(d, 0, sizeof(d));
d[0] = 1;
for(i = 1; i <= n; i++){
for(j = k; j >= 1; j--){
d[j] = d[j] + d[j-1] * (b[i] - j + 1);
}
}
d[k].print();
}
return 0;
}
												

sgu - 269 - Rooks的更多相关文章

  1. SGU 269. Rooks(DP)

    题意: 给n(<=250)条水平网格,然后在上面放k棋子,每行每列都只能放一个.求方法总数. Solution: 简单的DP, 只要对给出的水平长度排个序就很容易处理了. 需要用到高精度. 偷懒 ...

  2. SGU 222.Little Rooks

    题意: 求在n*n(n<10)的棋盘上放k个车(水平竖直行走)的方案数. Solution SGU220的简化版.直接DP 显然当k>n时,ans=0; f[i][j]代表在前n行放了j个 ...

  3. SGU题目总结

    SGU还是个不错的题库...但是貌似水题也挺多的..有些题想出解法但是不想写代码, 就写在这里吧...不排除是我想简单想错了, 假如哪位神犇哪天发现请告诉我.. 101.Domino(2015.12. ...

  4. UVA - 11134 Fabled Rooks[贪心 问题分解]

    UVA - 11134 Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to t ...

  5. SGU 495. Kids and Prizes

    水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...

  6. ACM: SGU 101 Domino- 欧拉回路-并查集

    sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Desc ...

  7. 【SGU】495. Kids and Prizes

    http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...

  8. SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...

  9. (light OJ 1005) Rooks dp

    http://www.lightoj.com/volume_showproblem.php?problem=1005        PDF (English) Statistics Forum Tim ...

随机推荐

  1. poj 1201 Intervals(差分约束)

    做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...

  2. 【C#学习笔记】获得系统时间

    using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { Conso ...

  3. ffmpeg+rtsp+dss

    1. push stream to dss ffmpeg -f mpegts -re -i film.v -c:v libx264 -s 352x288 -aspect 4:3 -b:v 300k - ...

  4. JavaScript学习笔记(备忘录)

    ===运算符 判断数值和类型是否相等.如: console.log('s'==='s') //输出trueconsole.log('1'===1) //输出false

  5. Oracle日常维护脚本

    1.正常停库流程     ps -ef|grep LOCAL=NO|cut -c 9-15|xargs kill -9      shutdown immediate; 2.备份数据库     bac ...

  6. K2 blackpearl 流程开发(二)

    转:http://blog.csdn.net/gxiangzi/article/details/8444590 本来想一篇文章把流程开发介绍完的,后来发现实在是太多了,只好分成两部分了.上一篇很简单的 ...

  7. centos软件环境

    1,保持能链接外网和yum的可用性. 注意:yum配置项中最好:keepcache=1 2,yum install gcc, gcc-c++, make, cmake, 3, ntfs-3g wget ...

  8. MyBatis 入门到精通(三) 高级结果映射

    MyBatis的创建基于这样一个思想:数据库并不是您想怎样就怎样的.虽然我们希望所有的数据库遵守第三范式或BCNF(修正的第三范式),但它们不是.如果有一个数据库能够完美映射到所有应用程序,也将是非常 ...

  9. C++ STL算法系列3---求和:accumulate

    该算法在numeric头文件中定义. 假设vec是一个int型的vector对象,下面的代码: //sum the elements in vec starting the summation wit ...

  10. sqlserver能否调用webservice发送短信呢?

    上班的时候突然有一个想法,sqlserver能否调用webservice发送短信呢? 经过查找资料,终于找到了解决办法,现将步骤贴到下面: (1)开启sqlserver组件功能,如果不开启这个组件功能 ...