Reading comprehension (BestCoder Round #8) (hdoj 4990)
来源:互联网 发布:手机测量尺寸软件 编辑:程序博客网 时间:2024/05/21 08:49
题解引用自作者
当i是偶数时f[i]=2*f[i-1] 否则f[i]=2*f[i-1]+1那么我们考虑,f[2*i]=4*f[2*i-2]+2这样偶项就成为了一个独立的递推数列。令b[0]=0,b[i]=4*b[i-1]+2对于f当i为偶数时,计算b[i/2]就可以了。当i为奇数时,计算b[i/2]*2+1计算b[i]可以建立矩阵递推[4011] 最后右边乘上列向量(0,2)T ,上面那一项就是b[i]了。
代码by 第XXX个小号 --传送门
#include<iostream>#include<cstdio>#include<cstdlib>using namespace std;typedef long long ll;int m;class Matrix2{private:ll array[2][2];public:Matrix2 operator * (Matrix2 &b);/*[4,1][0,1](0,2)T*/Matrix2(){array[0][0] = 4;array[0][1] = 1;array[1][0] = 0;array[1][1] = 1;}Matrix2(int num){array[0][0] = num;array[0][1] = 0;array[1][0] = 0;array[1][1] = num;}ll& at(int i, int j){ return array[i][j]; }};Matrix2 Matrix2::operator *(Matrix2 &b){Matrix2 c(0);for (int i = 0; i<2; i++)for (int j = 0; j<2; j++)for (int k = 0; k<2; k++){c.at(i, j) += (at(i, k) * b.at(k, j)) % m;}return c;}ll calc(int n){if (n == 0)return 0;Matrix2 ans(1), b;while (n){if (n & 1){ans = ans*b;}b = b*b;n >>= 1;}return ans.at(0, 1) * 2;}int main(){int i, j, k;int n;while (scanf("%d%d", &n, &m) != EOF){Matrix2 a, b;ll ans = calc(n / 2);if (n & 1)ans = ans * 2 + 1;ans = ans%m;printf("%lld\n", ans);}}
0 0
- Reading comprehension (BestCoder Round #8) (hdoj 4990)
- hdu 4990 Reading comprehension(BestCoder Round #8 1002)
- BestCoder Round #8 B题 Reading comprehension
- BestCoder Round #8 1002 Reading comprehension[矩阵幂求解递推值]
- HDOJ 4990 Reading comprehension
- hdu 4990 Reading comprehension 矩阵快速幂or数论 bestcoder round 8b
- Bestcoder#8 && HDU4990 Reading comprehension(规律)
- 【矩阵快速幂】 HDOJ 4990 Reading comprehension
- HDOJ 4990 Reading comprehension(矩阵快速幂)
- 【BestCoder#8】【1002】【Reading comprehension】【题解】【数列推通项】
- 【矩阵快速幂-找规律】HDOJ Reading comprehension 4990
- hdoj 4990 Reading comprehension(矩阵快速幂)
- HDU 4990 / BC 8B Reading comprehension
- hdu 4990 Reading comprehension
- hdu 4990 Reading comprehension
- hdu 4990 Reading comprehension
- HDU 4990 Reading comprehension
- HDU 4990 Reading comprehension
- Codeforces 464C Substitutes in Number 同余定理+模拟
- 强大的QDataStream
- [English]形容词和名词
- 关于字符串中回文子串的思考
- Flow Control in TCP(tcp的流量控制实现)
- Reading comprehension (BestCoder Round #8) (hdoj 4990)
- JSON解析的时候遇到NSNull
- 过桥问题(美团2014笔试题)
- hdf4
- 数据结构----栈的操作
- TestNG开源插件Arrow介绍
- 时间序列拟合模型诊断
- 多线程编程
- Java事务