LightOJ 1070 - Algebraic Problem 矩阵高速幂
题链:http://lightoj.com/volume_showproblem.php?problem=1070
Time Limit: 2 second(s) | Memory Limit: 32 MB |
Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessarily have to be real numbers.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains three non-negative integers, p, q and n. Here p denotes the value of a+b and q denotes the value of ab. Each number in the input file
fits in a signed 32-bit integer. There will be no such input so that you have to find the value of 00.
Output
For each test case, print the case number and (an+bn) modulo 264.
Sample Input |
Output for Sample Input |
2 10 16 2 7 12 3 |
Case 1: 68 Case 2: 91 |
题意:
给你p=a+b, q=ab
算出 (a^n+b^)mod2^64
做法:
mod 2^64所以开 unsigned long long 。llu 即可了,达到上限会自己主动取模的。
然后就是公式了。我是在推公式中找到的规律。
a^2+b^2=(a+b)*(a+b)-2*a*b
a^3+b^3=(a^2+b^2)*(a+b)-a*b(a+b)
a^4+b^4=(a^3+b^3)*(a+b)-a*b(a^2+b^2)
设G(n)=a^n+b^n
G(n)=G(n-1)*p-G(G-2)*q
然后就是高速幂了。.
#include<stdio.h>
#include<string.h>
#define Matr 5 //矩阵大小,注意能小就小 矩阵从1開始 所以Matr 要+1 最大能够100
#define ll unsigned long long
struct mat//矩阵结构体。a表示内容,size大小 矩阵从1開始 但size不用加一
{
ll a[Matr][Matr];
mat()//构造函数
{
memset(a,0,sizeof(a));
}
};
int Size= 2; mat multi(mat m1,mat m2)//两个相等矩阵的乘法,对于稀疏矩阵,有0处不用运算的优化
{
mat ans=mat();
for(int i=1;i<=Size;i++)
for(int j=1;j<=Size;j++)
if(m1.a[i][j])//稀疏矩阵优化
for(int k=1;k<=Size;k++)
ans.a[i][k]=(ans.a[i][k]+m1.a[i][j]*m2.a[j][k]); //i行k列第j项
return ans;
} mat quickmulti(mat m,ll n)//二分高速幂
{
mat ans=mat();
int i;
for(i=1;i<=Size;i++)ans.a[i][i]=1;
while(n)
{
if(n&1)ans=multi(m,ans);//奇乘偶子乘 挺好记的.
m=multi(m,m);
n>>=1;
}
return ans;
} void print(mat m)//输出矩阵信息。debug用
{
int i,j;
printf("%d\n",Size);
for(i=1;i<=Size;i++)
{
for(j=1;j<=Size;j++)
printf("%llu ",m.a[i][j]);
printf("\n");
}
}
int main()
{
/*
ll a,b;
while(scanf("%llu",&a)!=EOF)
printf("%llu\n",-a+18446744073709551615+1);
*/
int t;
int cas=1;
scanf("%d",&t);
while(t--)
{
ll p,q,n;
int p1,q1;
scanf("%lld%lld%llu",&p,&q,&n);// p a+b q ab ll tem=18446744073709551615-q+1; mat gouzao=mat(),chu=mat();//构造矩阵 初始矩阵
chu.a[1][1]=p;
chu.a[1][2]=p*p+2*tem;
chu.a[1][3]=q;
printf("Case %d: ",cas++);
if(n==0)
printf("2\n");
else if(n==1)
printf("%llu\n",p);
else if(n==2)
printf("%llu\n",p*p+2*tem);
else
{
gouzao.a[1][1]=0;
gouzao.a[2][1]=1;
gouzao.a[1][2]=tem;
gouzao.a[2][2]=p;
//print(gouzao);
printf("%llu\n",multi(chu,quickmulti(gouzao,n-2)).a[1][2]);
}
}
return 0;
} /*
ans^=n -
mat ans=mat();
ans.size=Size;
初始化ans矩阵
ans=quickmulti(ans,n,mod); void print(mat m)//输出矩阵信息。debug用
{
int i,j;
printf(%dn,m.size);
for(i=1;i=m.size;i++)
{
for(j=1;j=m.size;j++)printf(%d ,m.a[i][j]);
printf(n);
}
}
*/
LightOJ 1070 - Algebraic Problem 矩阵高速幂的更多相关文章
- LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)
题目链接:problem=1070">LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 ...
- LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导
题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...
- LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \(( ...
- HDU 2256 Problem of Precision(矩阵高速幂)
题目地址:HDU 2256 思路: (sqrt(2)+sqrt(3))^2*n=(5+2*sqrt(6))^n; 这时要注意到(5+2*sqrt(6))^n总能够表示成an+bn*sqrt(6); a ...
- [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3048 Accepted: 12 ...
- HDOJ 4686 Arc of Dream 矩阵高速幂
矩阵高速幂: 依据关系够建矩阵 , 高速幂解决. Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/ ...
- poj 2778 AC自己主动机 + 矩阵高速幂
// poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)
题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...
- HDOJ 4549 M斐波那契数列 费马小定理+矩阵高速幂
MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i ) ( i>=3) mod 1000000007 是质数 , 依据费马小定理 a^phi( p ) = 1 ( ...
随机推荐
- Codeforces 863F - Almost Permutation
863F - Almost Permutation 题意 给出每个位置可以放的数字的范围,定义 \(cost = \sum_{i=1}^{n}(cnt(i))^2\) ,其中 \(cnt(i)\) 为 ...
- CentOS下MySQL主从复制,读写分离
1.环境:所有系统都是CentOS5.5 mysql-5.6.31-2.el5,MySQL中都没有数据 主服务器IP为192.168.128.230 从服务器IP为192.168.128.235 代理 ...
- Linux下 编译C++/C以及常用的几种命令(ubuntu)
http://blog.csdn.net/bob1993_dev/article/details/45973919
- 10.2(java学习笔记)JDBC事务简述
一.事务 事务是指作为一系列操作组成的一个整体,该整体只有两种状态,要么全部执行,要么全部不执行. 当组成这个事务的所有语句都执行成功则该事务执行,只要有一条语句执行失败则该事务不执行. 假设这里有一 ...
- STL之全排列
描述 使用STL中的next_permutation函数输出一个序列的全排列. 部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码. int main() { vector<int> ...
- Problem D: 零起点学算法24——判断奇偶数
#include<stdio.h> int main() { int a; while(scanf("%d",&a)!=EOF) ==) printf(&quo ...
- SqlMapConfig.xml详细介绍
1,连接数据库 <!--配置环境,默认的环境id为oracle --> <environments default="oracle"> <!-- 配置 ...
- 修复XCode7 Beta版无法使用iOS8.4真机调试的Bug
在XCode7 Beta2下如果使用iOS8.4版的真机进行调试,XCode会提示: "Could not find Developer Disk Image" 解 ...
- 如何在mac中通过命令行使用sublime
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/bin/subl 后续就可以通过 ...
- Linux查找某个时间点后生成的文件(转)
需要找到某天(例如2017-04-13)以及这之后生成的空文件.那么这个要怎么处理呢?这个当然是用find命令来解决.如下所示, -mtime -5表示查找距现在5*24H内修改过的文件 -type ...