CodeForces 1152F1 Neko Rules the Catniverse (Small Version)
题目链接:http://codeforces.com/problemset/problem/1152/F1
题目大意
有 n 个星球,给定限制 m,从 x 星球走到 y 星球的条件是,$1 \leq y \leq x + m$,且 y 不能被访问过。
求游玩其中 k 个星球有多少种不同的方案?
分析
- 不访问:dp[i + 1][j][newsta] += dp[i][j][sta],newsta = (sta << 1) % (1 << m)。
- 访问:dp[i + 1][j + 1][newsta] += dp[i][j][sta] * (1 + 后m个星球被访问过的星球个数),newsta = ((sta << 1) % (1 << m)) | 1。
初始状态为 dp[0][0][0] = 1,dp[0][j][sta] = 0。
PS:newsta 可能对应多种不同的访问星球集合,所以要用 += 而不能用 =。
代码如下
时间复杂度:$O(n*k*2^m)$
#include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; void add_mod(LL &a, LL b) {
a = (a + b) % mod;
} int n, k, m;
// dp[i][j][k] 表示在前 i 个星球中,选 j 个,第 i-m-1 ~ i 的选取状态的二进制表示为 k 时的方案数
LL dp[maxN][][];
LL ans; int main(){
INIT();
cin >> n >> k >> m;
int maxSta = << m;
dp[][][] = ; Rep(i, n) {
Rep(j, k + ) {
Rep(sta, maxSta) {
int newsta = (sta << ) % maxSta;
// 不选 i + 1 个星球
add_mod(dp[i + ][j][newsta], dp[i][j][sta]);
// 选 i + 1 个星球
if (j < k) {
LL insertWays = __builtin_popcount(sta) + ;
add_mod(dp[i + ][j + ][newsta | ], insertWays * dp[i][j][sta]);
}
}
}
} Rep(sta, maxSta) add_mod(ans, dp[n][k][sta]);
cout << ans << endl;
return ;
}
CodeForces 1152F1 Neko Rules the Catniverse (Small Version)的更多相关文章
- CodeForces 1152F2 Neko Rules the Catniverse (Large Version)
题目链接:http://codeforces.com/problemset/problem/1152/F2 题目大意 见http://codeforces.com/problemset/problem ...
- Codeforces Round #554 (Div. 2) F2. Neko Rules the Catniverse (Large Version) (矩阵快速幂 状压DP)
题意 有nnn个点,每个点只能走到编号在[1,min(n+m,1)][1,min(n+m,1)][1,min(n+m,1)]范围内的点.求路径长度恰好为kkk的简单路径(一个点最多走一次)数. 1≤n ...
- 【CF1152F】Neko Rules the Catniverse(动态规划)
[CF1152F]Neko Rules the Catniverse(动态规划) 题面 CF 题解 我们先考虑一个需要扫一遍所有位置的做法. 那么状态一定是\(f[i]\)然后什么什么表示考虑到当前第 ...
- CF1152 F. Neko Rules the Catniverse (dp)
题意 一条长为 \(n\) 的数轴,可以从任意整点 \(\in [1, n]\) 出发,假设当前在 \(x\) ,下一步能到达的点 \(y\) 需要满足,\(y\) 从未到过,且 \(1 \le y ...
- Codeforces Round #535 E2-Array and Segments (Hard version)
Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- Codeforces Round #584 E2. Rotate Columns (hard version)
链接: https://codeforces.com/contest/1209/problem/E2 题意: This is a harder version of the problem. The ...
- codeforces#1152D. Neko and Aki's Prank(dp)
题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...
- codeforces#1152C. Neko does Maths(最小公倍数)
题目链接: http://codeforces.com/contest/1152/problem/C 题意: 给出两个数$a$和$b$ 找一个$k(k\geq 0)$得到最小的$LCM(a+k,b+k ...
随机推荐
- Eclipse中安装SVN插件的艰难旅程
我们写Java程序的人都知道Eclipse,也装过一些插件,比如Android开发的使用需要安装ADT等,如果代码提交的话我们可能需要安装git和svn的插件,但是这个插件我以前听过,但是一直没有安装 ...
- ubuntu 下gcc的编译运行
一些基本的操作 $gcc test.c //将test.c预处理.汇编.编译并链接形成可执行文件test $gcc test.c -o test //-o用来指定输出文件的文件名 $gcc -E te ...
- renren-fast-vue-动态路由
在renren-fast-vue项目中,左侧边栏的系统管理这一模块的路由采用的是动态路由的写法, 模块中的路由内容由后台动态生成,在前端开发阶段,采用的是mock模拟数据生成 先是在左侧边栏(view ...
- DELPHI中枚举类型数据的介绍和使用方法
在看delphi程序的时候看到aa=(a,b,c,d);这样的东西,还以为是数组,同事说是函数,呵呵,当然这两个都不屑一击,原来这样式子是在声明并付值一个枚举类型的数据.下边写下来DELPHI中枚举类 ...
- [NOIP模拟测试3] 建造游乐园 题解(欧拉图性质)
Orz 出题人石二队爷 我们可以先求出有n个点的联通欧拉图数量,然后使它删或增一条边得到我们要求的方案 也就是让它乘上$C_n^2$ (n个点里选2个点,要么删边要么连边,选择唯一) 那么接下来就是求 ...
- XMPP的总体架构和Jabber ID
通常XMPP的架构用C/S来实现,但是也并不是强制的,Client和Server,Server和Server之间通过TCP连接来通信. 架构的简单示意图如下: C1-----S1----S2----C ...
- (2)centos7 图形界面
1.登录 2.先安装MATE可视化桌面 yum groups install "MATE Desktop" 选择y 3.安装X Window System:图形接口 yum gro ...
- tensorflow 训练网络loss突然出现nan的情况
1.问题描述:开始训练一切都是那么的平静,很正常! 突然loss变为nan,瞬间懵逼! 2.在网上看了一些解答,可能是梯度爆炸,可能是有关于0的计算.然后我觉得可能是关于0的吧,然后进行了验证. 3. ...
- centos7下利用nfs搭建wordpress
拓扑环境 web1 192.168.198.110 web2 192.168.198.120 mysql 192.168.198.130 DNS 192.168.198.10 NFS 192.168. ...
- js关于if()else{}中的判定条件的认识,各种数据类型转换为Boolean类型的转换规则
博客搬迁,给你带来的不便敬请谅解! http://www.suanliutudousi.com/2017/09/24/js%E5%85%B3%E4%BA%8Eifelse%E4%B8%AD%E7%9A ...