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 ( ...
随机推荐
- HDU 多校1.7
Function Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- HDU 1856 More is better (并查集)
题意: 给你两个数代表这两个人是朋友,朋友的朋友还是朋友~~,问这些人组成的集合里面人最多的是多少... 思路: 属于并查集了,我用的是带路径压缩的,一个集合里面所有元素(除了根节点)的父节点都是根节 ...
- SDL安装小结
SDL是一个基于C的简易实现,安装过程中也多亏了,各位大神的助攻,这里简单mark一下遇到的问题,以备查找: 关于VS的版本:目前文档里确定支持的VS为2008到2013,我的VS是2013,2015 ...
- KMP+差分 文章过滤器 (filter)
Description 给定一些短串,要求你在一个长串中,将这些短串部分变为\(*\) Input 第一行包括一个整数\(n\),表示短串的数量. 接下来的\(n\)行,为\(n\)个短串. 最后一行 ...
- EditText中禁止输入中文的方法
应用场景 在Android应用中有时需要EditText中只允许输入约定的一些字符,禁止输入其他字符.这里列举了一些可能的应用场景. 1. 场景一 在通讯录保存好友信息界面中填写好友的电话号码时,应当 ...
- Java读取文本文件
try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw StringBuilder stringBuilder = new StringBuilder(); // 读入 ...
- SQL Server 事务隔离级别的解析
近来在项目中遇到的一些有关事务的问题,跟同事间讨论了一下,后面翻看了一些书籍和做了一些测试,趁有点时间把它写下来,一来加深印象,二来希望对大家有所帮助,当然,由于自身水平问题,如理解有误,还请大牛指出 ...
- 调试手机上网页 (断点 console timeline 选择dom)
用手机看网页,越来越多,手机app套个webview的也很多,那该如何调试手机上的页面了?比如 断点,选dom,console,控制台输出,查看内存,== 嗯,万能的的chrome和safari还是帮 ...
- CSS3:box-sizing 怪异盒模型
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ext js layout and tree
数据 <configuration> <configSections> <section name="hibernate-configuration&q ...
