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]);
    
  • 相关阅读:
    React在componentDidMount里面发送请求
    React 术语词汇表
    React里受控与非受控组件
    React和Vue等框架什么时候操作DOM
    【LeetCode】79. Word Search
    【LeetCode】91. Decode Ways
    【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
    【LeetCode】1. Two Sum
    【LeetCode】141. Linked List Cycle (2 solutions)
    【LeetCode】120. Triangle (3 solutions)
  • 原文地址:https://www.cnblogs.com/greyqz/p/11827201.html
Copyright © 2011-2022 走看看