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 ...
随机推荐
- Android中如何搭建一个WebServer
今天终于把老大交代的任务搞完了,感觉收获挺多的,所以就写一篇来记录一下吧,首先还是来看一下,老大们的需求 需求: 希望移动端的用户标识(IMEI)和HTML页面的用户标识(Cookie)连接起来,其中 ...
- HDU 4279 Number 坑爹的迷之精度
题目描述 首先定义"special number": 如果对于一个数字B,存在一个数字A(0<A<=B),并同时满足 B%A=0 和 gcd(A,B) != 1 ,那么 ...
- 剑指offer——数组中出现次数超过一半的数字(c++)
题目描述数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2.如 ...
- 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_验证码扩展)
从别人那拷贝的 #region 定义和初始化配置字段 //用户存取验证码字符串 public string validationCode = String.Empty; //生成的验证码字符串 pub ...
- 在Linux下安装PyEmu
git clone https://github.com/OpenRCE/pydbg.git git clone https://github.com/OpenRCE/paimei.git libda ...
- JDK8新特性之重复注解
什么是重复注解 下面是JDK8中的重复注解(java.lang.annotation.Repeatable)定义的源码. @Documented @Retention(RetentionPolicy. ...
- 匈牙利算法实战codevs1022覆盖
1022 覆盖 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 有一个N×M的单位方格中 ...
- Windows下Cython使用(VS2017)
收到公众号推送文章“利用Cython为Python代码加速”后尝试在Windows平台下使用Cython,环境为Python3.5 + VS2017. 1. 简单尝试 1)新建hello.pyx文件, ...
- extern static和函数
#include <stdio.h> int sum(int a, int b); int main() { /************************************** ...
- JQ基本
jQuery的入口函数: 特点:1. 等着DOM结构渲染完毕即可执行内部代码,不必等到所有外部资源加载完毕,jQuery帮我们完成了封装. 2. 相当于原生js中的DOMContentLoaded. ...