zoukankan      html  css  js  c++  java
  • 9-10 November

    cout 和 printf 在 C++ 中的实现:四舍六入五随缘。比如 printf("%.0lf ", x=1.5) => 1。

    标准做法:printf("%d ", (int)(x+0.5))

    矩阵乘法

    (a_n=2a_{n-3}+a_{n-1}) 得矩阵转移方程:

    [egin{bmatrix} a_n & a_{n-1} & a_{n-2} end{bmatrix} = egin{pmatrix} 1 & 1 & 0 \ 0 & 0 & 1 \ 2 & 0 & 0 end{pmatrix} imes egin{bmatrix} a_{n-1} & a_{n-2} & a_{n-3} end{bmatrix} ]

    这里采用 [] 表示 (1 imes 3) 矩阵,() 表示 (3 imes 3) 矩阵。

    ll p[5][5], b[5][5], d[5][5], t[5][5];
    
    memset(p, 0, sizeof p), memset(b, 0, sizeof b), memset(d, 0, sizeof d);
    for (int i=1; i<=3; i++) b[i][i]=1ll;  // unit matrix
    p[1][1]=1ll, p[1][2]=1ll, p[1][3]=0ll;
    p[2][1]=0ll, p[2][2]=0ll, p[2][3]=1ll;
    p[3][1]=2ll, p[3][2]=0ll, p[3][3]=0ll; // transition matrix
    d[1][1]=6ll, d[1][2]=1ll, d[1][3]=3ll; // initial matrix, a_3, a_2, a_1
    
    // n<=3 特判
    if (n<=3) {printf("%d
    ", d[1][4-n]); return 0; }
    
    // 矩阵快速幂
    int K=n-3;
    while (K>0){
    	if (K&1) {
    		// (b) *= (p)
    		memset(t, 0, sizeof t);
    		for (int i=1; i<=3; i++)
    			for (int j=1; j<=3; j++)
    				for (int k=1; k<=3; k++)
    					t[i][j]=(t[i][j]+b[i][k]*p[k][j]) % mod;
    		memcpy(b, t, sizeof b);
    	}
    
    	// (p) = (p)^2
    	memset(t, 0, sizeof t);
    	for (int i=1; i<=3; i++)
    		for (int j=1; j<=3; j++)
    			for (int k=1; k<=3; k++)
    				t[i][j]=(t[i][j]+p[i][k]*p[k][j]) % mod;
    	memcpy(p, t, sizeof p);
    
    	K>>=1;
    }
    
    // [t] = [d] * (b)
    memset(t, 0, sizeof t);
    for (int i=1; i<=3; i++)
    	for (int j=1; j<=3; j++)
    		t[1][i]=(t[1][i]+d[1][j]*b[j][i]) % mod;
    printf("%lld
    ", t[1][1]);
    
  • 相关阅读:
    使用expect实现ssh免密码登陆
    vim不小心ctrl+Z解决办法
    bash 自动补全
    PHP缓存技术
    windows下安装python的easy_install工具
    python中用lxml解析html
    python中用xpath和xml.dom解析html
    申请使用sourceforge免费空间几点注意事项
    创建.htaccess文件
    静态资源放置于独立域名之下
  • 原文地址:https://www.cnblogs.com/greyqz/p/11827201.html
Copyright © 2011-2022 走看看