zoukankan      html  css  js  c++  java
  • UVA 10655 Contemplation! Algebra

    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 and q 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+bnfits in a signed 64-bit integer.

    可以推出a ^ n + b ^ n = (a + b)[a ^ ( n - 1) + b ^ (n - 1)] - ab[a ^ (n - 2) + b ^ (n - 2)]

        #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define LL long long
    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[0][1]=s.f[1][0]=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;
    }
    

      

  • 相关阅读:
    第十二章 满城搜索 [转载]
    第十三章 平安是福
    第三十章 就是他!
    CyberArticle和Live Writer的比较
    NET CMS 大全
    第十五章 禹皇门,震动!
    第六篇 匹马行天下 第二十三章 血流成河,悟!
    第二十八章 震天怒吼
    今天终于找了一个代理登陆了传说中的三个网站
    第十六章 夜深人静
  • 原文地址:https://www.cnblogs.com/Commence/p/4337151.html
Copyright © 2011-2022 走看看