uva 10655 - Contemplation! Algebra
---恢复内容开始---
Given the value of a+b and ab you will have to find the value of an+bn
给出a+b和a*b的值,再给出n求a^n+b^n的值.
Input
The input file contains several lines of inputs. Each line except the last line contains 3 non-negative integers p, q 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.
多组测试数据,每组给出3个数,依次为p、q、n。p代表a+b,q代表a*b。当p和q都为0是这组数组不处理。
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+bn fits in a signed 64-bit integer.
分析:
定义f(n)=an+bn,则有f(n)∗(a+b)=(an+bn)∗(a+b)=an+1+abn+ban+bn+1=f(n+1)+abf(n−1), 所以f(n+1)=(a+b)f(n)−abf(n−1)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxsize = 100;
typedef long long ll;
struct matrix{
ll f[2][2];
};
matrix mul(matrix a,matrix b)
{
ll i,j,k;
matrix c;
memset(c.f,0,sizeof(c.f));
for(i=0;i<2;i++)
for(j=0;j<2;j++)
for(k=0;k<2;k++)
c.f[i][j]+=a.f[i][k]*b.f[k][j];
return c;
} matrix ksm(matrix e,ll n)
{
matrix s;
s.f[0][0]=s.f[1][1]=1;
s.f[1][0]=s.f[0][1]=0;
while(n)
{
if(n&1)
s=mul(s,e);
e=mul(e,e);
n=n>>1;
}
return s;
}
matrix e;
int main()
{
ll p,q,n;
while(cin>>p>>q>>n)
{
if(n==0)
{
cout<<2<<endl;
continue;
}
e.f[0][0]=p;
e.f[0][1]=1;
e.f[1][0]=-q;
e.f[1][1]=0;
e=ksm(e,n-1);
ll ans;
ans=p*e.f[0][0]+2*e.f[1][0];
cout<<ans<<endl; }
return 0;
}
uva 10655 - Contemplation! Algebra的更多相关文章
- uva 10655 - Contemplation! Algebra(矩阵高速幂)
题目连接:uva 10655 - Contemplation! Algebra 题目大意:输入非负整数,p.q,n,求an+bn的值,当中a和b满足a+b=p,ab=q,注意a和b不一定是实数. 解题 ...
- UVa 10655 Contemplation! Algebra 矩阵快速幂
题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ...
- Contemplation! Algebra(矩阵快速幂,uva10655)
Problem EContemplation! AlgebraInput: Standard Input Output: Standard Output Time Limit: 1 Second Gi ...
- 【UVA10655】 Contemplation! Algebra
题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) ...
- UVA-10655 Contemplation! Algebra (矩阵)
题目大意:给出a+b的值和ab的值,求a^n+b^n的值. 题目分析:有种错误的方法是这样的:利用已知的两个方程联立,求解出a和b,进而求出答案.这种方法之所以错,是因为这种方法有局限性.联立之后会得 ...
- UVa 10655 n次方之和(矩阵快速幂)
https://vjudge.net/problem/UVA-10655 题意: 输入非负整数p,q,n,求a^n+b^n的值,其中a和b满足a+b=p,ab=q. 思路: 递推式转化成矩阵的规律: ...
- UVA10655 Contemplation! Algebra —— 推公式、矩阵快速幂
题目链接:https://vjudge.net/problem/UVA-10655 题意: a+b.ab的值分别为p.q,求a^n+b^n. 题解: 1.a.b未知,且直接求出a.b也不太实际. 2. ...
- 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 ...
- KUANGBIN带你飞
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题 //201 ...
随机推荐
- UVa 1394: And Then There Was One
设置一个数组Winner记录经典约瑟夫问题中的剩余者即可递归解决该问题. 注: 约瑟夫问题:有编号为0~n-1的n个人,从0号开始报数1,2,3......报到k的杀死,然后从下一个人开始继续报数1, ...
- 点击返回键退出popupwindow的方法
点击返回键退出popupwindow mPopupWindow.setFocusable(true); 这句非常重要,对背景不会有影响 mPopupWindow.setBackgroundDrawab ...
- 王立平-Android中对图像进行Base64编码
// ------------------base64-------------------// public String bitmaptoString(Bitmap bitmap) { // 将B ...
- asp.net 常用的3中身份验证
1. windows验证: IIS根据应用程序的设置来进行身份验证,要使用这中验证方式,必须禁止使用匿名用户登录. 2. Forms验证: 通过Cookies来保存用户凭证,对未登录的用户 重定向到自 ...
- 在linq查询环境下通过sql语句来访问数据库
接上一篇随笔 这里主要介绍在linq环境下,如果实现用sql来访问数据库,同时也介绍在EF框架中如何添加新的方法来访问数据库. 1.首先,在数据访问层(EF.DAO,EF.IDAO)中添加具体的函数来 ...
- WPF中当鼠标移到按钮上时,按钮的背景图片消失的问题
如果给按钮设置了背景图片,当鼠标移到按钮上的时候,按钮就好变成一个浅蓝色的按钮,背景图片就消失了,对于这个问题有很多解决方法,我只分享一下我的解决方法. 我第一次用的方式是在按钮中添加一个图片,不用背 ...
- oracle spoof用法
关于SPOOL(SPOOL是SQLPLUS的命令,不是SQL语法里面的东西.) 对于SPOOL数据的SQL,最好要自己定义格式,以方便程序直接导入,SQL语句如: select taskindex|| ...
- C++细节系列(零):零散记录
老规矩:记录细节,等待空余,再进行整理. 1:const,static,const static成员初始化. 1.const成员:只能在构造函数后的初始化列表中初始化 2.static成员:初始化在类 ...
- JSP中Filter中访问Spring管理的beans
@Override public void init(FilterConfig filterConfig) { //unchecked = filterConfig.getInitParameter ...
- 配置managed server
managed server往往是部署应用程序的server,所以最好在weblgoic上配置上managed server,不要把应用程序直接部署到admin server上. 一.受管服务器的创建 ...