hdu 2256 Problem of Precision 构造整数 + 矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=2256
题意:给定 n 求解
?
思路: , 令
,
那么 ,
得:
得转移矩阵:
但是上面求出来的并不是结果,并不是整数。需要加上, 由于
所以结果为
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define pb push_back
#define MK make_pair
#define A first
#define B second
#define clear0 (0xFFFFFFFE)
#define inf 0x3f3f3f3f
#define eps 1e-8
#define mod 1024
#define zero(x) (((x)>0?(x):-(x))<eps)
#define bitnum(a) __builtin_popcount(a)
#define lowbit(x) (x&(-x))
#define K(x) ((x)*(x))
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
template<typename T>
void read1(T &m)
{
T x = ,f = ;char ch = getchar();
while(ch <'' || ch >''){ if(ch == '-') f = -;ch=getchar(); }
while(ch >= '' && ch <= ''){ x = x* + ch - '';ch = getchar(); }
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
inline ll gcd(ll a,ll b){ return b == ? a: gcd(b,a%b); }
inline ll lcm(ll a,ll b){ return a/gcd(a,b)*b; }
template<class T1, class T2> inline void gmax(T1& a, T2 b){ if(a < b) a = b;}
template<class T1, class T2> inline void gmin(T1& a, T2 b){ if(a > b) a = b;} struct Matrix{
int row, col;
int m[][];
Matrix(int r,int c):row(r),col(c){ memset(m, , sizeof(m)); } bool unitMatrix(){
if(row != col) return false;
for(int i = ;i < row;i++) //方阵才有单位矩阵;
m[i][i] = ;
return true;
}
Matrix operator *(const Matrix& t){
Matrix res(row, t.col);
for(int i = ; i < row; i++)
for(int j = ;j < t.col;j++)
for(int k = ; k < col; k++)
res.m[i][j] = (res.m[i][j] + m[i][k]*t.m[k][j])% mod;
return res;
}
void print(){
for(int i = ;i < row; i++){
for(int j = ;j < col; j++)
printf("%d ",m[i][j]);
puts("");
}
}
}; Matrix pow(Matrix a, int n)
{
Matrix res(a.row, a.col);
res.unitMatrix();
while(n){
if(n & ) res = res*a;
a = a*a;
n >>= ;
}
return res;
}
int main()
{
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
Matrix mat(,);
mat.m[][] = , mat.m[][] = ;
mat.m[][] = , mat.m[][] = ;
int T, kase = ;
scanf("%d",&T);
while(T--){
int n;
read1(n);
Matrix res = pow(mat, n-);
Matrix tmp(,);
tmp.m[][] = , tmp.m[][] = ;
res = res*tmp;
printf("%d\n", (*res.m[][]-)% mod);
}
return ;
}
hdu 2256 Problem of Precision 构造整数 + 矩阵快速幂的更多相关文章
- HDU 2256 Problem of Precision (矩阵快速幂)(推算)
Problem of Precision Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 2256 Problem of Precision 数论矩阵快速幂
题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5 ...
- HDU 2256 Problem of Precision( 矩阵快速幂 )
链接:传送门 题意:求式子的值,并向下取整 思路: 然后使用矩阵快速幂进行求解 balabala:这道题主要是怎么将目标公式进行化简,化简到一个可以使用现有知识进行解决的一个过程!菜的扣脚...... ...
- hdu 2256 Problem of Precision
点击打开hdu 2256 思路: 矩阵快速幂 分析: 1 题目要求的是(sqrt(2)+sqrt(3))^2n %1024向下取整的值 3 这里很多人会直接认为结果等于(an+bn*sqrt(6))% ...
- 【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)
[解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下来就是矩阵的快速幂了. 神图: 给个大神的链接:构造类斐波那契数列的矩阵快速幂 ...
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- (hdu 6030) Happy Necklace 找规律+矩阵快速幂
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...
- 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)
题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...
- hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂
Mathematician QSC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- 【原创】C++链表如何像Python List一样支持多种数据类型
用过Python的码友都知道,Python中List支持多种数据类型,如下面代码所示链表li内的数据类型可以是整数,同时也可以是字符串,当然也可以是其他数据类型. 1: >>> li ...
- springMVC+hibernate构建项目
这几天要用到springMVC+spring+hibernate构建框架,使用的是eclipse今天闲下来把这些记录下来 首先要导入spring 的jar包和hibernate的jar包
- C#中Dictionary小记
使用C#中Dictionary的一下细节小记: 一.Dictionary.Add(key,value) 与 Dictionary[key]=value的区别: 如果Dictionary中已经有了key ...
- Centos7.5 搭建Red5视频直播服务器
一.安装java环境 yum install java-1.7.0-openjdk 详细步骤请参考 http://www.cnblogs.com/java-qzq/p/5845509.html 我的这 ...
- Java POI操作Excle工具类
用到了jxl.jar和poi.jar 一些基本的操作Excel的操作方法: import java.io.File; import java.io.FileInputStream; import ja ...
- Java多线程之线程池
现在是多核的时代,面向多核的编程很重要,因此基于java的并发和多线程开发非常重要. 线程池是于队列密切相关的,其中队列保存了所有等待执行的任务.工作者线程的任务很简单:从队列中获取一个任务,执行任务 ...
- 一个Itextsharp 批量添加图片到pdf 方法
这里我就直接把我的页面贴进来了 using System; using System.Collections.Generic; using System.Web; using System.Web.U ...
- 一个网站的head和body是如何进行优化的
我们知道任何一个网站都要被解析成html后,浏览器才能识别,换句话说,用任何一门技术做的网站,都是被浏览器解析成为html.因此我们必须懂得,一个html页面由三部分组成,那就是html的开始标签和结 ...
- OGNL 对象视图导航语言
[Object Graphics Navigate Language] 类似于EL(Expression Language)表达式, 可以帮助我们在配置文件.JSP中来获取对象的值 这门语言比EL功能 ...
- html5+css3第一屏滚屏动画效果
详细内容请点击 在线预览立即下载 html5+css3第一屏滚屏动画效果. 转载自:http://tympanus.net/codrops/2014/05/22/inspiration-for-art ...