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..复习下..太弱了都快不会做了.. 这个矩阵的乘法 ...
随机推荐
- java基础(十四)集合(一)
这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...
- vijosP1687 细菌总数
vijosP1687 细菌总数 链接:https://vijos.org/p/1687 [思路] 错排公式+高精度. 题目要求排列数目而且不能有Pi==i的情况出现,可以看出这正是1,2,3,4,5, ...
- linux 多线程基础3
一.线程属性 线程具有属性,用pthread_attr_t表示,在对该结构进行处理之前必须进行初始化,在使用后需要对其去除初始化.我们用pthread_attr_init函数对其初始化,用pthrea ...
- Get familiar with key Frameworks of ios
Frameworks make your life easier as an iOS Developer. They allow you to reuse code written by other ...
- Java的MongoDB驱动及读写策略
网上看见一篇博文,详细讲了MongoDB读写策略,将来生产会遇到类似的问题,转来备查. 指定新mongo实例: Mongo m = new Mongo(); Mongo m = new Mongo( ...
- 【转】linux下安装opencv
Installation in Linux These steps have been tested for Ubuntu 10.04 but should work with other distr ...
- 396. Rotate Function
一开始没察觉到0123 3012 2301 而不是 0123 1230 2301 的原因,所以也没找到规律,一怒之下brute-force.. public int maxRotateFunction ...
- Oracle的OFA架构
最优灵活体系结构(Optimal Flexible Architecture,简称OFA) OFA其实就是一种Oracle的一种规范,其意义就是用一种统一的给文件和文件夹的规则,和文件存放目录的规则做 ...
- jQuery之事件even
jQuery之事件 W3C:http://www.w3school.com.cn/jquery/jquery_ref_events.asp 一.事件列表 1.blur() 当失去焦点时触发 ...
- hdu 2821 Pusher(dfs)
Problem Description PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, ...