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. 介绍 Scratch 3.0:扩展编码创造力

    在过去十年中,全世界数百万儿童使用Scratch编写自己的互动游戏,故事,动画等. 这种磅礴的创造力激励我们继续扩展和改进Scratch,让世界各地的孩子都有新的机会用新技术创造性地表达自己. 今天, ...

  2. 移动端font-size适配方案

    概述 这是我研究移动端页面时的思考,记录下来供以后开发时参考,相信对其他人也有用.由于我写移动端页面写的还比较少,一些问题都还没遇到,所以我的这篇博文不免有些错误的地方,还请大佬多多指正. 这篇文章是 ...

  3. iOS-button利用block封装按钮事件【runtime 关联】

    用block封装最常用的就是网络请求的回调,其实也可以结合category封装button的按钮事件,同时利用runtime的对象关联: UIButton+wkjButton.h 文件 #import ...

  4. Alpha冲刺(3/10)——追光的人

    1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...

  5. Microsoft SQL Server on Linux 踩坑指南

    微软用 SQL Server 在 2016 年的时候搞了一个大新闻,宣传 Microsoft ❤️ Linux 打得一众软粉措手不及.但是这还是好事情,Linux 上也有好用的 SQL Server ...

  6. 通过XMLHttpRequest和jQuery两种方式实现ajax

    一.XMLHttpRequest实现获取数据 不使用jQuery实现页面不刷新获取内容的方式,我们这里采用XMLHttpRequest原生代码实现:js代码如下: //1.获取a节点,并为其添加Onc ...

  7. shell信号捕捉命令 trap

    trap 命令 tarp命令用于在接收到指定信号后要执行的动作,通常用途是在shell脚本被中断时完成清理工作.例如: 脚本在执行时按下CTRL+c时,将显示"program exit... ...

  8. (转)CentOS一键安装Nginx脚本

    原文:https://www.xiaoz.me/archives/10301 https://blog.slogra.com/post-676.html-----centos7一键安装nginx脚本

  9. nginx配置client_body_temp_path

    http://wiki.nginx.org/HttpCoreModule 中写道: 这里的client_body_temp_path是制定post上传的$_FILES上传的文件地址 后面的level1 ...

  10. NIO基础之同步、异步、阻塞、非阻塞

    这里区分几个概念,也是常见但是容易混淆的概念,就是标题中的同步.异步.阻塞.非阻塞. 一.同步与异步 同步与异步,关心的是消息通信的机制.也就是调用者和被调用者之间,消息是如何进行通知的.如果是调用者 ...