Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)
Problem D. GukiZ and Binary Operations
Solution
一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数。
没有连续的1的二进制序列的数目满足f[i]=f[i-1]+f[i-2],恰好是斐波那契数列。
数据范围在10^18,用矩阵加速计算,有连续的1的数目就用2^n-f[n+1]
最后枚举k的每一位,是1乘上2^n-f[n+1],是0乘上f[n+1]
注意以上需要满足 2^l>k。并且这里l的最大值为64,需要特判。
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ll;
const int N = ;
ll n, k, l, m; struct Mat {
ll mat[N + ][N + ];
} A, B; Mat operator * ( Mat a, Mat b )
{
Mat c;
memset ( c.mat, , sizeof c.mat );
for ( int k = ; k < N; k++ )
for ( int i = ; i < N; i++ )
for ( int j = ; j < N; j++ )
( c.mat[i][j] += ( a.mat[i][k] * b.mat[k][j] ) % m ) %= m;
return c;
} Mat operator ^ ( Mat a, ll pow )
{
Mat c;
for ( int i = ; i < N; i++ )
for ( int j = ; j < N; j++ )
c.mat[i][j] = ( i == j );
while ( pow ) {
if ( pow & ) c = c * a;
a = a * a;
pow >>= ;
}
return c;
} ll quickp( ll x )
{
ll s = , c = ;
while( x ) {
if( x & ) s = ( s * c ) % m;
c = ( c * c ) % m;
x >>= ;
}
return s;
}
int main()
{
ios::sync_with_stdio( );
Mat p, a;
p.mat[][] = , p.mat[][] = ;
p.mat[][] = , p.mat[][] = ;
a.mat[][] = , a.mat[][] = ;
a.mat[][] = , a.mat[][] = ;
cin >> n >> k >> l >> m; ll ans = ;
if( l == || ( 1uLL << l ) > k ) {
ans++;
p = p ^ n;
a = a * p;
ll t1 = a.mat[][], t2 = ( m + quickp( n ) - t1 ) % m;
for( int i = ; i < l; ++i ) {
if( k & ( 1uLL << i ) ) ans = ( ans * t2 ) % m;
else ans = ( ans * t1 ) % m;
}
} cout << ans%m << endl; }
Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)的更多相关文章
- 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 ...
- Codeforces Round #307 (Div. 2) D 矩阵快速幂+快速幂
D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...
- Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂
https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...
- Codeforces 514E Darth Vader and Tree 矩阵快速幂
Darth Vader and Tree 感觉是个很裸的矩阵快速幂, 搞个100 × 100 的矩阵, 直接转移就好啦. #include<bits/stdc++.h> #define L ...
- Codeforces 576D Flights for Regular Customers 矩阵快速幂+DP
题意: 给一个$n$点$m$边的连通图 每个边有一个权值$d$ 当且仅当当前走过的步数$\ge d$时 才可以走这条边 问从节点$1$到节点$n$的最短路 好神的一道题 直接写做法喽 首先我们对边按$ ...
- CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解
思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...
- codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...
- Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)
传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...
- Codeforces 696D Legen...(AC自动机 + 矩阵快速幂)
题目大概说给几个字符串,每个字符串都有一个开心值,一个串如果包含一次这些字符串就加上对应的开心值,问长度n的串开心值最多可以是多少. POJ2778..复习下..太弱了都快不会做了.. 这个矩阵的乘法 ...
随机推荐
- Opencl API解释(二)
欢迎关注,转载引用请注明 http://blog.csdn.net/leonwei/article/details/8909897 这里将更深入的说明一些OpenCL API的功能 1. 创建buff ...
- Does anyone successfully use USB drive in Windows7 guest?
Hi, Does anyone successfully use USB drive in Windows7 guest? If I pass a USB drive to Windows7 gues ...
- 关于附件控件隐藏后,在IE下不能上传,报“拒绝访问”
报错时的使用: @Html.TextBoxFor(m => m.FileName, new { style = "width:457px;", @readonly = &qu ...
- http协议和web本质
转载:http://www.cnblogs.com/dinglang/archive/2012/02/11/2346430.html http协议和web本质 当你在浏览器地址栏敲入“http://w ...
- Code Forces Gym 100886J Sockets(二分)
J - Sockets Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Valera ...
- 探索AutoLayout的本质和解决一些问题
最近频繁使用AutoLayout,记录下自己的一些发现和问题的解决(不是教程) 1.简介 Auto Layout 是苹果在 iOS 6中新引入的布局方式,旨在解决不同尺寸屏幕的适配问题. 屏幕适配工作 ...
- NOIP-2003 加分二叉树
题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都 ...
- MySQL linux二进制安装
200 ? "200px" : this.width)!important;} --> 介绍 1.创建用户和目录 groupadd mysql useradd -r -g m ...
- JS时间处理,获取天时分秒
//获取时间的天,小时,分钟,秒 function ToTime(second) { second = second / ; var result ; ) % ; ) % ; * )); ) { re ...
- 硝烟中的Scrum和XP-我们如何实施Scrum 15)多团队 Part 2/2 16)地理分散 17)检查列表 18)其他
引入"团队领导"角色 假设有3个团队开发同一个产品 红色的P是PO, 黑色的S是SM, 蓝色是其他团队成员; 如何决定哪些人属于哪个团队? 怎么分配成员? 有人觉得让PO来做人员分 ...