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 ...
随机推荐
- Shell学习笔记 - Shell变量
一.变量的命名 变量名必须以字母或下划线开头,由字母.数字.或下划线组成,变量名的长度不能超过255个字符. 二.变量的分类 1. 用户自定义变量 2. 环境变量 3. 位置参数变量 4. 预定义变量 ...
- umbraco表单
view Model定义类 controller 创建Form @HTML.RenderPartial(“”,)
- 以NameValueCollection 修改URL中的查询参数
以NameValueCollection 修改URL中的查询参数 本文参考于:http://www.c-sharpcorner.com/Blogs/9421/add-remove-or-modify- ...
- Failed to load IDE add in 'C:\Program Files\Delphi_2007\bin\Borland.Studio.Together.dll'.解决办法 转
错误信息为: Failed to load IDE add in 'C:\Program Files\Delphi_2007\bin\Borland.Studio.Together.dll'.Exce ...
- poj 2029 二维树状数组
思路:简单树状数组 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...
- 自定义的UITabbar上面的按钮的x坐标的计算方法
; i < 4; i++) {//4是按钮的个数 NSString *backImage = backgroud[i]; NSString *heightImage = heightBackgr ...
- php中一串数子的转化
echo "<meta charset=utf-8>"; echo "笔试题测试"; $str = "123456780"; $ ...
- Table of Contents - Redis
Getting Started 安装配置环境 Redis 命令 Keys Strings Lists Hashs Sets Sorted Sets Transactions Connection Se ...
- java 集合(list、set、map)的特点
集合相关的类有一大堆,一般也只用到常用的方法增删改查,而且它它们的方法名也基本一样,所以一直都不知道什么时候用什么集合, 今天趁有空特意从网上整理资料方便日后回忆. 一.List:.有顺序以线性方式存 ...
- EL表达式运算符
语法:${运算表达式},EL表达式支持如下运算符 1.empty运算符:检查对象是否为null或“空”. 2.二元表达式:${user!=null?user.name : “”} . 3.[ ] 和 ...