UVa 10655 - Contemplation! Algebra

来源:互联网 发布:像一世之尊的小说知乎 编辑:程序博客网 时间:2024/05/16 12:39

初学矩阵 , 还没有习惯用矩阵的思路考虑问题 , 但貌似这个题不用矩阵思路更简单


提示:

1. 如何用a^( n/2 )+b^( n/2 ) 表示 a^n + b^n  呢


注意: 不能够使用unsigned long long 代替 long long , 答案可能是负数(因为a , b可能是虚数)

////  main.cpp//  UVa10655////  Created by Fuxey on 15/10/25.//  Copyright © 2015年 corn.crimsonresearch. All rights reserved.//#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <vector>#include <deque>#include <string>#include <list>#include <queue>#include <set>#include <map>#include <algorithm>using namespace std;typedef long long ull;ull p , q , n;ull powMul(ull n){    if(n==1) return q;    ull ans = powMul(n/2);    if(n%2) return ans*ans*q;    else return ans*ans;}ull powAnd(ull n){    if(n==1) return p;    else if(n==0) return 2;    ull halfs = powAnd(n/2);    if(n%2)    {        ull others = powAnd(n/2+1);        return others*halfs-powMul(n/2)*p;    }    else        return halfs*halfs-2*powMul(n/2);}int main(int argc, const char * argv[]) {        while(cin>>p>>q>>n) cout<<powAnd(n)<<endl;                return 0;}


0 0
原创粉丝点击