problem E
contemplation! Algebra
Input: standard input
output: standard outputTime
Limit: 1 Second
Given the value of a+b and ab you'll 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 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 and zeroes. This is should not being processed. Each number in the input file fits in a signed the-bit integer. There'll be is no such input so it has to find the value of the.
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 this an+bn fits in a signed 64-bit integer.
Sample Input Output for Samp Le Input
test instructions: Known p=a+b;q=a*b; A^n+b^n=ans? Note A, B is a real number! There is also the question that does not consider 0^0 this kind of situation! then analyze: n = 0, ans0 = 2;n = 1, ans1 = a + b = p;n = 2, ans2 = p * p-2 * q =ans1 * p - ans0 * q ;likewise n = 3, ans3=Ans2* P- ans1* q; n = 4, ... ..... .........so the matrix is
Topic Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=1596 Reprint Please specify the source: Look for & the star of the child to the problem is a bit more than the pit dad; that's the final set of test data ... Don't explain I was wrong 5 times ...
#include <cstring>//the input in C + + is over. #include <cstdio>#include<iostream>using namespacestd;#defineLL Long Longstructmatrix{LL mat[2][2];}; Matrix Multiply (Matrix A,matrix b) {matrix C; memset (C.mat,0,sizeof(C.mat)); for(intI=0;i<2; i++) { for(intj=0;j<2; j + +) { if(a.mat[i][j]==0)Continue; for(intk=0;k<2; k++) { if(b.mat[j][k]==0)Continue; C.MAT[I][K]+=a.mat[i][j]*B.mat[j][k]; } } } returnC;} Matrix Quickmod (Matrix a,ll m) {matrix res; for(intI=0;i<2; i++) for(intj=0;j<2; j + +) Res.mat[i][j]= (i==j); while(m) {if(m&1) res=multiply (res,a); M>>=1; A=multiply (a,a); } returnRes;}intMain () {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==0) printf ("2\n");//cout<<2<<endl;// Else if(n==1) printf ("%lld\n", p);//cout<<p<<endl;// Else if(n==2) printf ("%lld\n", p*p-2*Q);//cout<<p*p-2*q<<endl;// Else { //initial matrix (p*p-2*q,p)matrix ans; ans.mat[0][0]=p; ans.mat[0][1]=1; ans.mat[1][0]=-P; ans.mat[1][1]=0; Ans=quickmod (ans,n-1); LL Ant=p*ans.mat[0][0]+2*ans.mat[1][0]; //cout<<ant<<endl;printf"%lld\n", ant);//printf ("%lld\n", (P*ans.mat[0][0]+2*ans.mat[1][0])); } } return 0;}
Come on, boy!
contemplation! Algebra (Matrix fast Power, uva10655)