Codeforces 551 D. GukiZ and Binary Operations
\(>Codeforces \space 551 D. GukiZ and Binary Operations<\)
题目大意 :给出 \(n, \ k\) 求有多少个长度为 \(n\) 的序列 \(a\) 满足 \((a_1\ and \ a_2)or(a_2\ and \ a_3)or..or(a_{n-1}\ and \ a_n) = k\) 且 \(a_i \leq k \leq 2^l\)
并输出方案数在$\mod m $ 意义下的值
\(0≤ n ≤ 10^{18},\ 0 ≤ k ≤ 10^{18}, \ 0 \leq l \leq 64, \ 1 \leq m \leq 10^9 + 7\)
解题思路 :
考虑对于二进制按位拆开来考虑,设某一位最终为 \(i\) 的方案为 \(g_i \ (i = 0 / 1)\)
因为位与位之间相互不影响,由此可以得到 $Ans = \sum_{i = 0}^{l - 1} g_{(2^i and \space k)} $
问题转化为如何求出 \(g_i\), 观察发现 \(g_i\) 只要求出一个,另外一个就是 \(2^n - g_i\)
仔细分析后发现 \(g_0\) 比较好求,设 \(f_i\) 为前 \(i\) 位的式子的结果为 \(0\) 的方案
考虑第 \(i\) 位后答案若要为 \(0\) ,如果第 \(i\) 位选 \(1\),那么第 \(i - 1\) 位必然选 \(1\) ,方案数就是 \(f_{i-2}\)
否则第 \(i\) 位选 \(0\), 第 \(i-1\) 位选什么都可以,方案数是 \(f_{i-1}\) 所以有 \(f_i = f_{i-1} + f_{i-2}\)
发现式子其实就是斐波那契数列的递推式, 用矩阵快速幂求出后把得到的 \(g_0\) 和 \(g_1\) 带回先前的式子算出答案即可
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define int ll
int n, k, l, Mod;
const int le = 2;
struct Matrix{
int a[le+5][le+5];
inline Matrix(){ memset(a, 0, sizeof(a)); }
inline void write(){
for(int i = 1; i <= le; i++, putchar('\n'))
for(int j = 1; j <= le; j++) cout << a[i][j] << " ";
}
};
inline Matrix Mult(Matrix A, Matrix B){
Matrix C;
for(int i = 1; i <= le; i++)
for(int j = 1; j <= le; j++)
for(int k = 1; k <= le; k++)
(C.a[i][j] += A.a[i][k] * B.a[k][j]) %= Mod;
return C;
}
inline Matrix Power(Matrix a, int b){
Matrix ans = a; b--;
for(; b; b >>= 1, a = Mult(a, a))
if(b & 1) ans = Mult(ans, a);
return ans;
}
inline ll Pow(int a, int b){
int ans = 1;
for(; b; b >>= 1, a = a * a % Mod)
if(b & 1) ans = ans * a % Mod;
return ans;
}
main(){
read(n), read(k), read(l), read(Mod);
if(l < 63 && k >= (1ll << l)) return puts("0"), 0;
int all = Pow(2, n);
Matrix A, B;
A.a[1][1] = A.a[2][1] = 1;
B.a[1][1] = B.a[1][2] = B.a[2][1] = 1;
B = Power(B, n), A = Mult(B, A);
int now = (all - A.a[1][1] + Mod) % Mod, ans = 1 % Mod;
for(int i = 0; i < l; i++)
if((1ll << i) & k) (ans *= now) %= Mod;
else (ans *= A.a[1][1]) %= Mod;
cout << ans % Mod;
return 0;
}
Codeforces 551 D. GukiZ and Binary Operations的更多相关文章
- Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)
Problem D. GukiZ and Binary Operations Solution 一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数. 没有连续的1的二进制序列的 ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp
D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...
- D. GukiZ and Binary Operations(矩阵+二进制)
D. GukiZ and Binary Operations We all know that GukiZ often plays with arrays. Now he is thinking ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)
题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations
得到k二进制后,对每一位可取得的方法进行相乘即可,k的二进制形式每一位又分为2种0,1,0时,a数组必定要为一长为n的01串,且串中不出现连续的11,1时与前述情况是相反的. 且0时其方法总数为f(n ...
- GukiZ and Binary Operations CodeForces - 551D (组合计数)
大意: 给定$n,k,l,m$, 求有多少个长度为$n$, 元素全部严格小于$2^l$, 且满足 的序列. 刚开始想着暴力枚举当前or和上一个数二进制中$1$的分布, 但这样状态数是$O(64^3)$ ...
- codeforces 551 C GukiZ hates Boxes
--睡太晚了. ..脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...
- Codeforces 551 E - GukiZ and GukiZiana
E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...
- [codeforces 339]D. Xenia and Bit Operations
[codeforces 339]D. Xenia and Bit Operations 试题描述 Xenia the beginner programmer has a sequence a, con ...
随机推荐
- 【比赛】STSRM 09
第一题 题意:n个点,每个点坐标pi属性ai,从右往左将遇到的点向左ai范围内的点消除,后继续扫描. 现可以在扫描开始前提前消除从右往左任意点,问最少消除数(提前+扫描). n,pi,ai<=1 ...
- javascript 变量类型判断
一.typeof 操作符 对于Function, String, Number ,Undefined 等几种类型的对象来说,他完全可以胜任,但是为Array时 "); typeof arr ...
- ShellCode的几种调用方法
ShellCode是一种漏洞代码,中文名也叫填充数据,一般是用C语言或者汇编编写.在研究的过程中,自己也学到了一些东西,发现其中也有许多坑,所以贴出来,如果大家有碰到的,可以参考一下. 以启动电脑上的 ...
- Python3 面向对象编程高级语法
1.静态方法: #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:CarsonLi class Dog(object): def __init ...
- Python标准库笔记(4) — collections模块
这个模块提供几个非常有用的Python容器类型 1.容器 名称 功能描述 OrderedDict 保持了key插入顺序的dict namedtuple 生成可以使用名字来访问元素内容的tuple子类 ...
- 【Python学习笔记】Coursera之PY4E学习笔记——String
1.字符串合并 用“+”来进行字符串的合并,注意空格是要自己加的. 例: >>> a='Hello' >>> b= a+ 'There' >>> ...
- CSS浮动和清除
float:让元素浮动,取值:left(左浮动).right(右浮动) clear:清除浮动,取值:left(清除左浮动).right(清除右浮动).both(同时清除上面的左浮动和右浮动) 1.CS ...
- Oracle 获取ddl语句
--得到所有表空间的ddl语句 SELECT DBMS_METADATA.GET_DDL('TABLESPACE', TS.tablespace_name)FROM DBA_TABLESPACES T ...
- 2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 ...
- 数据库简述(以MySQL为例)
一.数据库中的概念 1.数据库是用户存放数据.访问数据.操作数据的存储仓库,用户的各种数据被有组织地存放在数据库中.可以随时被有权限的用户查询.统计.添加.删除和修改.可以说,数据库是长期存储在计算机 ...