hdu-2256(矩阵快速幂+推导)
来源:互联网 发布:郑州淘宝店铺装修设计 编辑:程序博客网 时间:2024/06/07 04:43
问题描述:
一句话题目:让你求(sqrt(2)+sqrt(3))^(2*n) mod 1024.
Input
The first line of input gives the number of cases, T. T test cases follow, each on a separate line. Each test case contains one positive integer n. (1 <= n <= 10^9)
Output
For each input case, you should output the answer in one line.
Sample Input
3125Sample Output
997841
题目分析:
先给出一个大佬的博客,自己太鶸了,这个公式和一开始的想法完全不一样,后来发现我最初的想法是错的(千万别想着用double 取模)
点击打开链接
然后就可以根据那个矩阵来写代码了。
代码如下:
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;const int mod=1024;struct matrix//构建矩阵{ int f[3][3]; matrix operator* (const matrix &a) const { matrix res; for (int i=1;i<=2;i++) { for (int j=1;j<=2;j++) { res.f[i][j]=0; for (int k=1;k<=2;k++) res.f[i][j]=(res.f[i][j]+(*this).f[i][k]*a.f[k][j])%mod; } } return res; }}a,b;void init()//预处理矩阵{ b.f[1][1]=5,b.f[1][2]=12; b.f[2][1]=2,b.f[2][2]=5; a.f[1][1]=5; a.f[2][1]=2;}matrix fast_pow(matrix base,int k)//快速幂{ matrix ans=base; while (k) { if (k&1) ans=base*ans; base=base*base; k>>=1; } return ans;}int main(){ int t; scanf("%d",&t); while (t--) { int n; scanf("%d",&n); if (n==1) { printf("%d\n",9); continue;} n-=2; init(); struct matrix cur,ans; cur=b; cur=fast_pow(cur,n); ans=cur*a; printf("%d\n",(2*ans.f[1][1]-1)%mod); } return 0;}
阅读全文
0 0
- hdu-2256(矩阵快速幂+推导)
- hdu 4565 公式推导+矩阵快速幂
- HDU 4565 (数学推导 矩阵快速幂)
- hdu-2842(矩阵快速幂+推导)
- hdu-4565(矩阵快速幂+推导)
- (矩阵快速幂, 数学公式推导)HDU 6050 Funny Function
- 快速矩阵幂HDU 2256
- hdu 2256(矩阵快速幂)
- hdu 2256(矩阵快速幂)
- HDU 2256 矩阵快速幂
- poj-3734(矩阵快速幂+推导)
- HDU 2256(矩阵快速幂)
- hdu 2256(矩阵快速幂)
- hdu 2256【矩阵快速幂 构造】
- HDU-2256(矩阵快速幂)
- hdu 2256 4565 矩阵快速幂
- WOJ1540 Fibonacci(数学推导,矩阵快速幂)
- LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)
- arduino实例1:led闪烁
- 请描述 BFC(Block Formatting Context) 及其如何工作
- 数人云|关于Docker Swarm&K8S,几大要素免踩坑
- 循环里使用嵌套集合的陷阱(小白会犯的错)
- caffe详细知识
- hdu-2256(矩阵快速幂+推导)
- dubbo超时与超时后自动重复调用的问题
- 零基础前端学习
- Cookie Session会话编程基础
- 4.12 leetcode -12 pascals-triangle
- MySQL命令行导出数据库
- JS单独文件写后JQuery生效方式
- 子串提取
- httpClient post发送json数据