Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)
2015-08-07 12:37
381 查看
转自http://www.cnblogs.com/keam37/p/4578568.html
View Code
/*Author :usedrose */ /*Created Time :2015/8/7 12:26:39*/ /*File Name :2.cpp*/ #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <iostream> #include <algorithm> #include <sstream> #include <cstdlib> #include <cstring> #include <climits> #include <vector> #include <string> #include <ctime> #include <cmath> #include <deque> #include <queue> #include <stack> #include <set> #include <map> #define INF 0x3f3f3f3f #define eps 1e-8 #define pi acos(-1.0) #define MAXN 1110 #define MAXM 100110 #define OK cout << "ok" << endl; #define o(a) cout << #a << " = " << a << endl #define o1(a,b) cout << #a << " = " << a << " " << #b << " = " << b << endl using namespace std; typedef long long ll; const int N = 2; ll n, k, l, m; struct Mat { ll mat[N + 1][N + 1]; } A, B; Mat operator * ( Mat a, Mat b ) { Mat c; memset ( c.mat, 0, sizeof c.mat ); for ( int k = 0; k < N; k++ ) for ( int i = 0; i < N; i++ ) for ( int j = 0; 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 = 0; i < N; i++ ) for ( int j = 0; j < N; j++ ) c.mat[i][j] = ( i == j ); while ( pow ) { if ( pow & 1 ) c = c * a; a = a * a; pow >>= 1; } return c; } ll quickp( ll x ) { ll s = 1, c = 2; while( x ) { if( x & 1 ) s = ( s * c ) % m; c = ( c * c ) % m; x >>= 1; } return s; } int main() { //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); cin.tie(0); ios::sync_with_stdio(false); Mat p, a; p.mat[0][0] = 0, p.mat[0][1] = 1; p.mat[1][0] = 1, p.mat[1][1] = 1; a.mat[0][0] = 1, a.mat[0][1] = 2; a.mat[1][0] = 0, a.mat[1][1] = 0; cin >> n >> k >> l >> m; ll ans = 0; if (l == 64 || (1uLL<<l) > k ) { ans++; p = p ^ n; a = a * p; ll t1 = a.mat[0][0], t2 = (m + quickp(n) - t1)%m; for (int i = 0;i < l; ++ i) { if ((1uLL<<i)&k) ans = (ans*t2)%m; else ans = (ans*t1)%m; } } cout << ans%m << endl; return 0; }
View Code
相关文章推荐
- qwt安装调试linux
- hdoj 4737 区间维护
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)新的异常解决方案
- 使用Apache 的 FTP Client获取FTP服务器上的文件列表FTP response 421 received.Server closed connection问题的解决
- Nginx版本的“helloworld”
- linux多进程和多线程的优缺点
- 在linux中配置tomcat
- centOS配置mysql+appache+vsftp=php服务器
- OpenGL三大变换
- Linux系统用户账号管理
- Mac下配置Apache服务
- nginx 使用
- Openssl 生成证书
- Linux文件夹/文件基本操作
- Ubuntu和centos安装vsftpd步骤教程-ftp
- Linux 数据库学习的准备工作---win通过ssh访问ubuntu
- Tomcat 自动部署
- 下载tomcat8w双击打开时候显示指定的服务未安装和用myeclipse配置tomcata
- 如何配置tomcat服务器
- window下键盘监控api函数详解