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..复习下..太弱了都快不会做了.. 这个矩阵的乘法 ...
随机推荐
- [CODEVS1294]全排列
题目描述 Description 给出一个n, 请输出n的所有全排列 输入描述 Input Description 读入仅一个整数n (1<=n<=10) 输出描述 Output De ...
- bzoj 1027 [JSOI2007]合金(计算几何+floyd最小环)
1027: [JSOI2007]合金 Time Limit: 4 Sec Memory Limit: 162 MBSubmit: 2970 Solved: 787[Submit][Status][ ...
- 在Dubbo中开发REST风格的远程调用(RESTful Remoting)
rest 在Dubbo中开发REST风格的远程调用(RESTful Remoting)
- Yii常用技巧总结
//YII framework路径 Yii::getFrameworkPath(); //protected/runtime Yii::app()->getRuntimePath(); //pr ...
- hdoj 1827 Summer Holiday【强连通分量&&缩点】
Summer Holiday Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- sublime中安装sublimelinter php 语法检查
打开控制台,install package 搜 sublimelinter 先安装sublimelinter本体 安装完以后再搜索一下,安装sublimelinter-php 接下来,打开prefer ...
- (转)JS托管和最新的jQuery引用地址大全(jQuery最新版v1.9.0)
什么是Google的js托管? 说的明白点,跟我们以往做法一样,只不过这时候的引用的js库是放在Google服务器上的.比如引用jquery,则使用路径http://ajax.googleapis.c ...
- JSTL 数字日期标签库
<frm:formatNumber/>标签 该标签依据特定的区域将数字改变为不同的格式来显示. 被格式化的数据<frm:formatNumber> <fmt:format ...
- ibatis 开发中的经验 (一)ibatis 和hibernate 在开发中的理解
这个项目的底层是用ibatis框架完毕,这几天也是都在用这个框架写代码,也有了一些简单的理解,把项目拿过来后基本的配置都已经配置好了,比方一些事务.日志控制等,在开发中主要用到的是写SQL语句以及熟悉 ...
- 在Eclipse中怎样公布创建的JavaWebproject
博客<在Eclipse中怎样创建JavaWebproject>中图文并茂的说明了Eclipse中创建JavaWebproject的方法:博客<怎样为Eclipse开发工具中创建的Ja ...