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 ...
随机推荐
- Service3
RAID阵列概述• 廉价冗余磁盘阵列– 通过硬件/软件技术,将多个较小/低速的磁盘整合成一个大磁盘– 阵列的价值:提升I/O效率.硬件级别的数据冗余– 不同RAID级别的功能.特性各不相同 ##### ...
- TFS——更改计算机名称,影响TFS使用
今天把自己电脑的计算机名称改了,打开VS的时候,就提示以下的错误: 报错情况 显示错误:工作区 DADI--20141015Q;SD-SERVER\Administrator 未驻留在此计算机上.如果 ...
- (4)centos7 基础命令
1.显示文件列表 ls 显示当前目录下所有非隐藏的文件夹名称 -a #显示路径下所有文件及目录 (包括以.开头的隐藏文件) -l #除文件名称外,亦将文件型态.权限.拥有者.文件大小等资讯详细列出(不 ...
- docker IPv4 forwarding is disabled. 解决方法
问题 最近在 docker 部署 django 项目打包镜像时遇到 [root@localhost ~]# docker build -t test1 . ...省略... WARNING: IPv4 ...
- 浅谈HP-Socket在物联网的应用
原文链接:https://my.oschina.net/chrisforbt/blog/1669746 一.应用背景 去年公司成立了个项目——<智慧用电安全隐患监管服务平台>,计划是开发一 ...
- JVM简介及类加载机制(一)
JVM介绍: 目标:JVM运行字节码文件,根据JVM的日志调节程序,对于底层原理有一定的了解 1. 类加载 在JAVA代码中,类型的加载,连接与初始化都是在程序运行期间完成的,提供了灵活性增加了更多的 ...
- 前端(二)—— CSS的引入方式、长度与颜色单位、常用样式、选择器
CSS的引入方式.长度与颜色单位.常用样式.选择器 一.CSS的三种引入方式 1.行间式 <!doctype html> <html> <head> <met ...
- Java随机访问文件
使用随机访问文件,我们可以从文件读取以及写入文件.使用文件输入和输出流的读取和写入是顺序过程.使用随机访问文件,可以在文件中的任何位置读取或写入.RandomAccessFile类的一个对象可以进行随 ...
- Java对图片压缩
背景:图片上传服务器时候的大小限制取消之后,上传图片太大导致前台显示加载缓慢 需求:服务器对接收到的图片进行压缩 方法:1.上传后的文件保存在临时文件夹“/usr/upload/tmp” 2.压 ...
- 关于Web中的图标使用问题
挺懒的,这里做个记录. Web中的图标使用问题 : https://github.com/amfe/article/issues/2