HDU 4990 / BC 8B Reading comprehension
来源:互联网 发布:数据科技vps 编辑:程序博客网 时间:2024/06/04 20:36
题意要求输出题目中代码的输出
代码很好理解 i从1到n i为奇数时ans=ans*2+1 i为偶数时ans*=2
可以把两步操作合成到一起 即 ans=ans*4+2 然后执行n/2次 (n为奇数就再补上一次即可)
然后用矩阵快速幂就可以了
#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#include<set>#include<stack>#define bug puts("bugbugbug");using namespace std;typedef long long ll;const int maxn = 20;const int maxm = 20;ll mod, k;struct Matrix{ int n, m; long long a[maxn][maxm]; void clear() { n = m = 0; memset(a, 0, sizeof(a)); } Matrix operator * (const Matrix &b) const //实现矩阵乘法 { Matrix tmp; tmp.n = n; tmp.m = b.m; for (int i = 0; i < n; i++) for (int j = 0; j < b.m; j++) tmp.a[i][j] = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (!a[i][j]) continue; for (int k = 0; k < b.m; k++) tmp.a[i][k] += a[i][j] * b.a[j][k], tmp.a[i][k] %= mod; } return tmp; } void Copy(const Matrix &b) { n = b.n, m = b.m; for (int i = 0; i < n; i++) for(int j = 0; j < m; j++) a[i][j] = b.a[i][j]; } void unit(int sz) { n = m = sz; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) a[i][j] = 0; a[i][i] = 1; } }};Matrix A, B,C,D;void init(){ A.clear(); A.n=A.m=2; A.a[0][0]=4; A.a[0][1]=2; A.a[1][0]=0; A.a[1][1]=1; D.clear(); D.n=D.m=2; D.a[0][0]=2; D.a[0][1]=1; D.a[1][0]=0; D.a[1][1]=1; B.clear(); B.n = 2; B.m = 1; //矩阵B是f(x)的前10个数 B.a[0][0] = 0; B.a[1][0]=1;}Matrix Matrix_pow(Matrix A, ll k, ll mod) //矩阵快速幂{ Matrix res; res.clear(); res.n = res.m = A.n; for (int i = 0; i < A.n; i++) res.a[i][i] = 1; while(k) { if (k & 1) res.Copy(A * res); k >>= 1; A.Copy(A * A); } return res;}int main(){ long long n,m; init(); while(~scanf("%lld%lld",&n,&m)) { mod=m; int flag=0; if(n%2) { n-=1; flag++; } C.Copy(Matrix_pow(A,n/2,m)); C.Copy(C*B); if(flag) C.a[0][0]=(C.a[0][0]*2+1)%m; printf("%d\n",C.a[0][0]); } return 0;}
0 0
- HDU 4990 / BC 8B Reading comprehension
- 【CUGBACM15级BC第8场 B】hdu 4990 Reading comprehension
- hdu 4990 Reading comprehension
- hdu 4990 Reading comprehension
- hdu 4990 Reading comprehension
- HDU 4990 Reading comprehension
- HDU 4990 Reading comprehension
- HDU 4990 Reading comprehension
- hdu 4990 Reading comprehension
- hdu-4990 Reading comprehension
- HDU 4990 Reading comprehension
- hdu 4990 Reading comprehension 矩阵快速幂or数论 bestcoder round 8b
- BestCoder Round #8 B题 Reading comprehension
- hdu 4990 Reading comprehension(BestCoder Round #8 1002)
- hdu 4990 Reading comprehension (矩阵快速幂)
- hdu 4990 Reading comprehension(矩阵快速幂)
- hdu 4990 Reading comprehension(矩阵法)
- [矩阵快速幂] hdu 4990 Reading comprehension
- 多线程的几种加锁方式详解
- LINQ排序,join on,分组查询,any和all量词
- 对于handler内存泄漏分析解决+实例
- yarn-cluster运行spark streaming问题
- matlab显示并保存RGB图像的单个通道图像
- HDU 4990 / BC 8B Reading comprehension
- URL与资源
- 使用和写简单的Makefile文件
- NFS根文件系统的制作
- 省市联动的简单实现
- poj2992(Divisors)(阶乘因数分解)
- 支付宝支付流程
- ActionBar扩展
- linux系统日志与网络管理