题目链接:http://codeforces.com/problemset/problem/1152/F2

题目大意

  见http://codeforces.com/problemset/problem/1152/F1,此题 n 最大能到 109

分析

  在 F1 的基础上,我们发现 dp[i + 1] 数组的每个值均可以通过 dp[i] 数组的有限几个数求得,而 dp[i + 2] 数组的每个值也均可以通过 dp[i + 1] 数组相同位置的有限几个数求得,于是我们可以考虑用矩阵快速幂来求 dp[n]。
  由于 dp 数组的第二维和第三维数值并不是特别大,我们可以把 dp 数组的后两个维度压制成一个维度,于是 dp[i][j][sta] 就变成 dp[i][cur],其中 cur = j * (1 << m) + sta。
  假设 dp[i][cur] 对某一个 dp[i + 1][newcur] 有贡献,根据上一题的帖子,有两种可能:
  1. 不访问:dp[i + 1][newcur] += dp[i][cur]。
  2. 访问:dp[i + 1][newcur] += dp[i][cur] * (1 + 后m个星球被访问过的星球个数)。

  定义 base 矩阵是我们所要构造的矩阵,设 base[cur][newcur] 代表某一个状态 cur 对一个新状态 newcur 所做贡献的系数,于是有:

  1. 不访问:base[cur][newcur] = 1。
  2. 访问:base[cur][newcur] =  (1 + 后m个星球被访问过的星球个数)。
  3. 其他(cur 压根不可能变成 newcur):base[cur][newcur] = 0。
  于是 $dp[i + 1][newcur] = \sum_{cur = 0}^{maxCur} dp[i][cur] * base[cur][newcur]$。
  根据这个式子可以构造 dp 矩阵如下(以 k = 1, m = 1 为例):
$$
dp(i) = \begin{bmatrix}
dp[i][0] & dp[i][1] & dp[i][2] & dp[i][3]
\end{bmatrix} 
$$
  相应 base 系数矩阵如下:

$$
base = \begin{bmatrix}
base[0][0] & base[0][1] & base[0][2] & base[0][3] \\
base[1][0] & base[1][1] & base[1][2] & base[1][3] \\
base[2][0] & base[2][1] & base[2][2] & base[2][3] \\
base[3][0] & base[3][1] & base[3][2] & base[3][3]
\end{bmatrix}
$$

  base 矩阵填上数后为:

$$
base = \begin{bmatrix}
1 & 1 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 1 & 1 \\
1 & 2 & 0 & 0
\end{bmatrix}
$$

  于是有:

$$
\begin{align*}
dp(i) &= dp(i - 1) * base \\
dp(n) &= dp(0) * base^{n}
\end{align*}
$$

  不知道关于矩阵怎么构造的说的明不明白,有不足的地方还请指出。
  PS:如果不压制维度,那么 base 矩阵就要弄四维的了,四维矩阵乘法反正我是不会。

代码如下

  时间复杂度:$O(\log n*(k*2^m)^3)$

 #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;
} struct Matrix{
int row, col;
LL MOD;
VVL mat; Matrix(int r, int c, LL p = mod) : row(r), col(c), MOD(p) {
mat.assign(r, VL(c, ));
}
Matrix(const Matrix &x, LL p = mod) : MOD(p){
mat = x.mat;
row = x.row;
col = x.col;
}
Matrix(const VVL &A, LL p = mod) : MOD(p){
mat = A;
row = A.size();
col = A[].size();
} // x * 单位阵
inline void E(int x = ) {
assert(row == col);
Rep(i, row) mat[i][i] = x;
} inline VL& operator[] (int x) {
assert(x >= && x < row);
return mat[x];
} inline Matrix operator= (const VVL &x) {
row = x.size();
col = x[].size();
mat = x;
return *this;
} inline Matrix operator+ (const Matrix &x) {
assert(row == x.row && col == x.col);
Matrix ret(row, col);
Rep(i, row) {
Rep(j, col) {
ret.mat[i][j] = mat[i][j] + x.mat[i][j];
ret.mat[i][j] %= MOD;
}
}
return ret;
} inline Matrix operator* (const Matrix &x) {
assert(col == x.row);
Matrix ret(row, x.col);
Rep(k, x.col) {
Rep(i, row) {
if(mat[i][k] == ) continue;
Rep(j, x.col) {
ret.mat[i][j] += mat[i][k] * x.mat[k][j];
ret.mat[i][j] %= MOD;
}
}
}
return ret;
} inline Matrix operator*= (const Matrix &x) { return *this = *this * x; }
inline Matrix operator+= (const Matrix &x) { return *this = *this + x; } inline void print() {
Rep(i, row) {
Rep(j, col) {
cout << mat[i][j] << " ";
}
cout << endl;
}
}
}; // 矩阵快速幂,计算x^y
inline Matrix mat_pow_mod(Matrix x, LL y) {
Matrix ret(x.row, x.col);
ret.E();
while(y){
if(y & ) ret *= x;
x *= x;
y >>= ;
}
return ret;
} int n, k, m, maxSta;
LL ans; int toId(int j, int sta) {
return j * maxSta + sta;
} int main(){
INIT();
cin >> n >> k >> m;
maxSta = << m;
Matrix dp(, (k + ) * maxSta);
dp.mat[][] = ;
Matrix base((k + ) * maxSta, (k + ) * maxSta); // 构造base矩阵
Rep(j, k + ) {
Rep(sta, maxSta) {
int newsta = (sta << ) % maxSta;
int cur = toId(j, sta), newcur = toId(j, newsta); // 不选 i + 1 个星球从cur->newcur的状态变化
base.mat[cur][newcur] = ;
// 选 i + 1 个星球从cur->newcur的状态变化
if (j < k) {
newcur = toId(j + , newsta | );
base.mat[cur][newcur] = __builtin_popcount(sta) + ;
}
}
} base = mat_pow_mod(base, n);
dp *= base; Rep(sta, maxSta) add_mod(ans, dp.mat[][toId(k, sta)]);
cout << ans << endl;
return ;
}

CodeForces 1152F2 Neko Rules the Catniverse (Large Version)的更多相关文章

  1. 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 ...

  2. CodeForces 1152F1 Neko Rules the Catniverse (Small Version)

    题目链接:http://codeforces.com/problemset/problem/1152/F1 题目大意 有 n 个星球,给定限制 m,从 x 星球走到 y 星球的条件是,$1 \leq ...

  3. 【CF1152F】Neko Rules the Catniverse(动态规划)

    [CF1152F]Neko Rules the Catniverse(动态规划) 题面 CF 题解 我们先考虑一个需要扫一遍所有位置的做法. 那么状态一定是\(f[i]\)然后什么什么表示考虑到当前第 ...

  4. CF1152 F. Neko Rules the Catniverse (dp)

    题意 一条长为 \(n\) 的数轴,可以从任意整点 \(\in [1, n]\) 出发,假设当前在 \(x\) ,下一步能到达的点 \(y\) 需要满足,\(y\) 从未到过,且 \(1 \le y ...

  5. Codeforces Round #535 E2-Array and Segments (Hard version)

    Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...

  6. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  7. 【36.86%】【codeforces 558B】Amr and The Large Array

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. Codeforces Round #584 E2. Rotate Columns (hard version)

    链接: https://codeforces.com/contest/1209/problem/E2 题意: This is a harder version of the problem. The ...

  9. codeforces#1152D. Neko and Aki's Prank(dp)

    题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...

随机推荐

  1. elementUi-复选框,使用v-for循环出来的复选框,默认多个值为勾选状态

    1. 使用 v-model="BottomSelectFor[index].tick" 绑定要默认勾选的状态 2.在数组中定义 tick:true,没有的字段默认为false 3. ...

  2. Vue2.0源码思维导图-------------Vue 构造函数、原型、静态属性和方法

    已经用vue有一段时间了,最近花一些时间去阅读Vue源码,看源码的同时便于理解,会用工具画下结构图. 今天把最近看到总结的结构图分享出来.希望可以帮助和其他同学一起进步.当然里边可能存在一些疏漏的,或 ...

  3. BZOJ 4698: Sdoi2008 Sandy的卡片(后缀数组+差分+二分答案)

    传送门 解题思路 看到一个子串加一个数字到另一个子串,自然可以想到差分.然后要把所有串都拼起来,求出\(height\)数组后可以二分答案来做,每次二分一个答案后统计一下连续的\(height> ...

  4. VC内联汇编,引用程序中的变量

    int a=5; //变量a _asm { mov eax,a;       //将变量a的值放入寄存器eax add eax,eax;   //相当于a=a+a mov a,eax;      // ...

  5. MProtect使用小计【三】 – 权限管理

    说明 本篇简单的说一下怎么样使用的VMProtect的权限管理功能,使我们的程序拥有注册码的功能.只用的注册版的程序才能执行指定的函数. 同样这个功能VMProtect也有例子位置在:安装目录\VMP ...

  6. SAS市场研究应用介绍:组合/联合分析

    SAS市场研究应用介绍:组合/联合分析 一 SAS市场研究模块介绍 市场研究是指研究组织(企业)与客户.公众三者关系的规律的过程,是市场营销领域中的一个重要元素.它把消费者.客户.公众和营销者通过信息 ...

  7. C++——变量

    1.变量的初始化和赋值 初始化:创建变量时赋予一个初始值 赋值:把变量的当前值擦除,以新的值替代 2.变量的声明和定义 声明:名字为程序所知.如果一个程序要使用另一个程序的名字,则要包含对那个名字的声 ...

  8. PHP-FPM 远程代码执行漏洞(CVE-2019-11043)复现-含EXP

    搭建容器 安装golang 利用程序 https://github.com/neex/phuip-fpizdam 安装git Cobra包安装 go get -v github.com/spf13/c ...

  9. 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷

    通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下:     步骤1:通过MyEclipse中的window->show View->ot ...

  10. java-Day01

    什么是java语言 1995由美国SUN公司推出的高级编程语言(后2009年甲骨文公司收购SUN公司) 常用的DOS命令 盘符切换命令:盘符: 查看当前文件夹:dir 进入文件命令:cd 文件夹名 退 ...