GukiZ and Binary Operations CodeForces - 551D (组合计数)
大意: 给定$n,k,l,m$, 求有多少个长度为$n$, 元素全部严格小于$2^l$, 且满足
的序列.
刚开始想着暴力枚举当前or和上一个数二进制中$1$的分布, 但这样状态数是$O(64^3)$在加上矩阵幂的复杂度显然不行.
看了题解发现可以按每位单独来考虑.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head ll n, k, l, m; struct Mat {
int v[4][4];
Mat() {memset(v, 0, sizeof v);}
Mat operator * (const Mat& b) const {
Mat c;
REP(k,0,3) REP(i,0,3) REP(j,0,3) {
c.v[i][j] = ((ll)v[i][k]*b.v[k][j]+c.v[i][j])%m;
}
return c;
}
Mat operator ^ (ll nn) {
Mat b, a=*this;
REP(i,0,3) b.v[i][i]=1;
while(nn) {
if(nn&1LL) b=b*a;
nn>>=1LL,a=a*a;
}
return b;
}
}; int main() {
cin>>n>>k>>l>>m;
if (m==1||l<64&&(k>>l)) return puts("0"),0;
Mat g;
g.v[0][0]=g.v[0][2]=g.v[1][1]=g.v[1][3]=g.v[2][0]=g.v[3][1]=g.v[3][2]=g.v[3][3]=1;
g = g^n;
int x = (g.v[0][0]+g.v[2][0])%m, y = (g.v[1][0]+g.v[3][0])%m;
ll ans = 1;
REP(i,0,l-1) {
if (k>>i&1) ans = ans*y%m;
else ans = ans*x%m;
}
printf("%lld\n", ans);
}
GukiZ and Binary Operations CodeForces - 551D (组合计数)的更多相关文章
- Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)
Problem D. GukiZ and Binary Operations Solution 一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数. 没有连续的1的二进制序列的 ...
- Codeforces 551 D. GukiZ and Binary Operations
\(>Codeforces \space 551 D. GukiZ and Binary Operations<\) 题目大意 :给出 \(n, \ k\) 求有多少个长度为 \(n\) ...
- 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 ...
- Intercity Travelling CodeForces - 1009E (组合计数)
大意: 有一段$n$千米的路, 每一次走$1$千米, 每走完一次可以休息一次, 每连续走$x$次, 消耗$a[1]+...+a[x]$的能量. 休息随机, 求消耗能量的期望$\times 2^{n-1 ...
- Yet Another Problem On a Subsequence CodeForces - 1000D (组合计数)
大意:定义一个长为$k>1$且首项为$k-1$的区间为好区间. 定义一个能划分为若干个好区间的序列为好序列. 给定序列$a$, 求有多少个子序列为好序列. 刚开始一直没想出来怎么避免重复计数, ...
- Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)
大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...
随机推荐
- 在iOS开发中使用icon font的方法
http://iconfont.cn/help/iconuse.html 在开发阿里数据iOS版客户端的时候,由于项目进度很紧,项目里的所有图标都是用最平常的背景图片方案来实现.而为了要兼容普通屏与R ...
- linux下编译利用CMakeLists.txt 编译C++写的opencv程序
https://hihozhou.com/blog/2017/05/11/linux-compile-opencv-c++-file.html cmake . make -j8
- Mybatis-Plus BaseMapper自动生成SQL及MapperProxy
目录 Spring+Mybatis + Mybatis-Plus 自定义无XML的sql生成及MapperProxy代理生成 问题产生背景 框架是如何使用 无Xml的SQL是如何生成生成及SQL长成什 ...
- Linux下出现Permission denied解决
今天不想写前言,直接写解决办法 输入命令设置root密码 sudo passwd 得到的答复是 We trust you have received the usual lecture from th ...
- nginx 实现高并发和高负载
一.Nginx是如何实现高并发的 service nginx start之后,然后输入#ps -ef|grep nginx,会发现Nginx有一个master进程和若干个worker进程,这些work ...
- 20191121-5 Scrum立会报告+燃尽图 01
此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/10065 一.小组情况 组长:贺敬文组员:彭思雨 王志文 位军营 徐丽君队名 ...
- 字符串暴力枚举子序列求LCS
题意: 求n个串里的LCS,长度相同时按照字典序排序 solution: 断环为链,二进制枚举子序列,压入vector,按照字典序排序 把出现次数为n的,压入第二个vector 输出最长的第二个vec ...
- LeetCode 44. 通配符匹配(Wildcard Matching)
题目描述 给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配. '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个字符串完 ...
- Undo Segment/Undo Retention
undo_retention简单定义,就是最多数据的最少保留时间.AUM模式下,undo_retention参数用于事务commit后undo数据保留的时间.单位为秒.这是个no guarantee的 ...
- nginx 反向代理实现负载均衡*配置实战
重要点: 1配置反向代理多虚拟主机节点服务器 2经过反向代理后的节点服务器记录用户IP 3与反向代理配置相关的更多参数说明 4根据URL目录地址转发 (1)根据URL中的目录地址实现代理转发(动静分离 ...