Problem E
Contemplation! Algebra
Input: Standard Input

Output: Standard Output

Time Limit: 1 Second

Given the value of a+b and ab you will have to find the value of an+bn

Input

The input file contains several lines of inputs. Each line except the last line contains 3 non-negative integers pq and n. Here p denotes the value of a+b andq denotes the value of ab. Input is terminated by a line containing only two zeroes. This line should not be processed. 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 line of input except the last one produce one line of output. This line contains the value of an+bn.  You can always assume that an+bfits in a signed 64-bit integer.

            Sample Input                                                                               Output for Sample Input

10 16 2 
7 12 3 
0 0

68

91 

题意:已知p=a+b;q=a*b;求a^n+b^n=ans? 注意a,b是实数哦!还有题目说不用考虑0^0这种情况!

那么分析一下:n = 0 ,  ans= 2 ;

       n = 1 ,  ans= a + b = p ;

       n = 2 ,  ans= p * p - 2 * q = ans1 * p - ans0 * q;

  同理   n = 3 ,  ans= ans2 * p - ans1 * q;    

       n = 4 ,       .......

       ..................

       所以矩阵为

p

1

-q

0

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1596

转载请注明出处:寻找&星空の孩子

对了这个题还有一点比较坑爹;就是最后那组测试数据。。。不解释我错了5次。。。

#include<cstring>//用c++的输入就过了。
#include<cstdio>
#include<iostream>
using namespace std; #define LL long long struct matrix
{
LL mat[][];
}; matrix multiply(matrix a,matrix b)
{
matrix c;
memset(c.mat,,sizeof(c.mat));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(a.mat[i][j]==)continue;
for(int k=;k<;k++)
{
if(b.mat[j][k]==)continue;
c.mat[i][k]+=a.mat[i][j]*b.mat[j][k];
}
}
}
return c;
} matrix quickmod(matrix a,LL m)
{
matrix res;
for(int i=;i<;i++)
for(int j=;j<;j++)
res.mat[i][j]=(i==j);
while(m)
{
if(m&) res=multiply(res,a);
m>>=;
a=multiply(a,a);
}
return res;
} int main()
{
LL p,q,n;
// while(scanf("%lld%lld%lld",&p,&q,&n),p+q+n)
while(cin>>p>>q>>n)
{
// if(!n&&(!p||!q)) break; // scanf("%lld",&n);
if(n==)printf("2\n");//cout<<2<<endl;//
else if(n==)printf("%lld\n",p);// cout<<p<<endl;//
else if(n==) printf("%lld\n",p*p-*q);//cout<<p*p-2*q<<endl;//
else
{
//初始矩阵(p*p-2*q,p)
matrix ans;
ans.mat[][]=p;
ans.mat[][]=;
ans.mat[][]=-q;
ans.mat[][]=; ans=quickmod(ans,n-);
LL ant=p*ans.mat[][]+*ans.mat[][];
// cout<<ant<<endl;
printf("%lld\n",ant);
// printf("%lld\n",(p*ans.mat[0][0]+2*ans.mat[1][0]));
}
}
return ;
}

加油,少年!!!

Contemplation! Algebra(矩阵快速幂,uva10655)的更多相关文章

  1. Contemplation! Algebra 矩阵快速幂

    Given the value of a+b and ab you will have to find the value of a n + b n Input The input file cont ...

  2. UVa 10655 Contemplation! Algebra 矩阵快速幂

    题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ...

  3. uva 10655 - Contemplation! Algebra(矩阵高速幂)

    题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ...

  4. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  5. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  6. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  7. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  8. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

  9. 51nod 1126 矩阵快速幂 水

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

随机推荐

  1. AEAI WM v1.6.0 升级说明,开源工作管理系统

    1 升级说明 AEAI WM v1.6.0版是AEAI WM v1.5.0版工作管理系统的升级版本,本次升级的系统是基于AEAI DP 3.8.0_20170228进行打包部署的,对产品中的功能及BU ...

  2. “全栈2019”Java多线程第二十九章:可重入锁与不可重入锁详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  3. 利用Knockoutjs对电话号码进行验证

    问题来源 最近在项目中前端使用Knockoutjs,验证模块自然也是使用Knockoutjs来进行表单验证了,比较头痛,因为没有使用过Knockoutjs,更加别说要去用它做表单验证了,于是乎恶补了一 ...

  4. Mac 安装mongodb

    http://blog.csdn.net/u010311313/article/details/46948995 1.前往官网下载MongoDB压缩包 2.将下载好的压缩包解压,将解压出的文件夹下的内 ...

  5. vue教程3-05 vue组件数据传递、父子组件数据获取,slot,router路由

    vue教程3-05 vue组件数据传递 一.vue默认情况下,子组件也没法访问父组件数据 <!DOCTYPE html> <html lang="en"> ...

  6. c++获取随机数

    方法一: 使用 rand 函数可以获取,如下. 随机数大小是在0到RAND_MAX,值为2147483647,它是在stdlib中定义的,如果我们希望在某个范围内,可以使用 % 结合 / 来实现. 但 ...

  7. sql字符处理

    --Description: 字符处理 --使用: 放到查询分析器里执行就可以了 --示例: select * from dbo.splitstr('12 44 45 50 56 87',' ') o ...

  8. Java 中的队列 Queue

    一.队列的定义 我们都知道队列(Queue)是一种先进先出(FIFO)的数据结构,Java中定义了java.util.Queue接口用来表示队列.Java中的Queue与List.Set属于同一个级别 ...

  9. Leetcode 763. Partition Labels

    思路:动态规划.对于属于coins的coin,只要知道amount-coin至少需要多少个货币就能表示,那么amount需要的货币数目=amount-coin需要的货币数目+1:如果amount-co ...

  10. Haproxy 重定向跳转设置 - 运维小结

    前面已经详细介绍了Haproxy基础知识 , 今天这里再赘述下Haproxy的重定向跳转的设置.  haproxy利用acl来实现haproxy动静分离,然而在许多运维应用环境中,可能需要将访问的站点 ...