Greg and Friends

BFS的过程中维护一下方案数。 我个人感觉不是很好想, 但是写出来之后怎么感觉这题这么SB啊啊。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, k, cnt[];
int dp[N][N][];
LL comb[N][N];
LL ans[N][N][]; int main() {
for(int i = ; i < N; i++)
for(int j = comb[i][] = ; j <= i; j++)
comb[i][j] = (comb[i - ][j - ] + comb[i - ][j]) % mod;
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++) {
int x; scanf("%d", &x);
if(x == ) cnt[]++;
else cnt[]++;
}
memset(dp, -, sizeof(dp));
queue<pair<PII, int>> que;
que.push(mk(mk(, ), ));
ans[][][] = ;
dp[][][] = ;
while(!que.empty()) {
int x = que.front().fi.fi;
int y = que.front().fi.se;
int op = que.front().se;
que.pop();
if(op) {
for(int i = ; i <= min(k / , x); i++) {
int up = min(y, (k - i * ) / );
for(int j = ; j <= up; j++) {
if(!i && !j) continue;
if(dp[x - i][y - j][] == - || dp[x - i][y - j][] == dp[x][y][] + ) {
if(dp[x - i][y - j][] == -) que.push(mk(mk(x - i, y - j), op ^ ));
dp[x - i][y - j][] = dp[x][y][] + ;
ans[x - i][y - j][] = (ans[x - i][y - j][] + ans[x][y][] * comb[x][i] % mod * comb[y][j] % mod) % mod;
}
}
}
} else {
for(int i = ; i <= min(k / , cnt[] - x); i++) {
int up = min(cnt[] - y, (k - i * ) / );
for(int j = ; j <= up; j++) {
if(!i && !j) continue;
if(dp[x + i][y + j][] == - || dp[x + i][y + j][] == dp[x][y][] + ) {
if(dp[x + i][y + j][] == -) que.push(mk(mk(x + i, y + j), op ^ ));
dp[x + i][y + j][] = dp[x][y][] + ;
ans[x + i][y + j][] = (ans[x + i][y + j][] + ans[x][y][] * comb[cnt[]-x][i] % mod * comb[cnt[]-y][j] % mod) % mod;
}
}
}
}
}
if(dp[cnt[]][cnt[]][] == -) {
puts("-1");
puts("");
} else {
printf("%d\n", dp[cnt[]][cnt[]][]);
printf("%lld\n", ans[cnt[]][cnt[]][]);
}
return ;
} /*
*/

Codeforces 295C Greg and Friends BFS的更多相关文章

  1. codeforces 295C Greg and Friends(BFS+DP)

    One day Greg and his friends were walking in the forest. Overall there were n people walking, includ ...

  2. Codeforces 295C Greg and Friends

    BFS+DP.dp[i][j][0]表示有i个50kg,j个100kg的人在左岸,dp[i][j][1]表示有i个50kg,j个100kg的人在右岸.用BFS求最短路的时候记录到达该状态的可能情况. ...

  3. ACM - 最短路 - CodeForces 295B Greg and Graph

    CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...

  4. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  5. CodeForces 540C Ice Cave (BFS)

    http://codeforces.com/problemset/problem/540/C       Ice Cave Time Limit:2000MS     Memory Limit:262 ...

  6. codeforces 1283D. Christmas Trees(bfs)

    链接: https://codeforces.com/contest/1283/problem/D 题意:给定n个不同的整数点,让你找m个不同的整数点,使得这m个点到到这n个点最小距离之和最小. 思路 ...

  7. codeforces Gargari and Permutations(DAG+BFS)

    /* 题意:求出多个全排列的lcs! 思路:因为是全排列,所以每一行的每一个数字都不会重复,所以如果有每一个全排列的数字 i 都在数字 j的前面,那么i, j建立一条有向边! 最后用bfs遍历整个图, ...

  8. CodeForces 689B Mike and Shortcuts (BFS or 最短路)

    题目链接:http://codeforces.com/problemset/problem/689/B 题目大意: 留坑 明天中秋~

  9. Codeforces 295A Greg and Array

    传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. buildroot构建项目(八)--- u-boot 2017.11 适配开发板修改 5 ---- 系统启动初始化之五

    执行完 board_init_f 后,跳回到 crt0.S中继续执行汇编语言 ldr r0, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp, ...

  2. Mac改键软件Karabiner使用教程

    Mac改键软件Karabiner使用教程 目前Mac上比较好用的改键软件是Karabiner,不过对于最新的Sierra系统,Karabiner失效了.这里介绍的实际上是Karabiner-Eleme ...

  3. 单选按钮QRadioButton

    继承  QAbstractButton import sys from PyQt5.QtGui import QPixmap from PyQt5.QtWidgets import QApplicat ...

  4. php rsa理解

    参考链接:http://www.cnblogs.com/firstForEver/p/5803940.html 自己封装的一个类: <?php class CRsaAuthorization { ...

  5. MySQL内连接、外连接、交叉连接

    外连接: 左连接:left join 或 left outer join 以左边的表为基准,如果左表有数据,而右表没有数据,左表的数据正常显示,右表数据显示为空. 创建user表,用于记录用户 use ...

  6. Linux内核驱动--硬件访问I/O【原创】

    寄存器与内存 寄存器与内存的区别在哪里呢? 寄存器和RAM的主要不同在于寄存器操作有副作用(side effect或边际效果): 读取某个地址时可能导致该地址内容发生变化,比如很多设备的中断状态寄存器 ...

  7. C# 清理消息管道的消息

    using System;using System.Collections.Generic;using System.Linq;using System.Messaging;using System. ...

  8. keepalived实现haproxy负载均衡器的高可用

    一.keepalived简介 keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于,用来防止单点故障. 二.vrrp协议2.1 vrrp协议简介 在现实的网络环境中,两台需要通信 ...

  9. centos系统中perl进程病毒占用大量网络流量导致网络瘫痪的问题分析及解决方案

    故障现象: 1.系统在早上9点的时候非常慢,单台服务器占用流量很大,使交换机流量被占满,而连累挂在同一交换机上的其他应用也无法提供服务,或者速度非常慢     2.通过查看进程发现大量的perl程序占 ...

  10. 关于报错stale element reference: element is not attached to the page document处理

    1.现象 在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element ref ...