Tuesday, March 9, 2010

SAMPLE QUESTIONS AND ANSWERS 2

Powers Et Al.

Problem G
Power et al.
Input: Standard Input
Output: Standard Output

Finding the exponent of any number can be very troublesome as it grows exponentially J. But in this problem you will have to do a very simple task. Given two non-negative numbers m and n, you will have to find the last digit of mn in decimal number system.

Input
The input file contains less than 100000 lines. Each line contains two integers m and n (Less than 10^101). Input is terminated by a line containing two zeroes. This line should not be processed.

Output
For each set of input you must produce one line of output which contains a single digit. This digit is the last digit of mn.
Sample Input Output for Sample Input

2 2 4
2 5 2
0 0

_______________________________________________________________________________________
Solution :

#include
#include
#include
#include
#include
#include
using namespace std;

long square(long n){ return n*n; }
long fastexp (long base, long power){
if(power ==0)
cout<<"1"<else if (power%2 == 0)
cout<else
cout<
}
long m,n,r,last;
long ary[4] = {4,1,2,3};
int main(){
//freopen("inputA6.txt","r",stdin);
//freopen("inputA6Out.txt","w",stdout);

cin>>m;
cin>>n;
while(m !=0 || n !=0){

// if(n==0)
// cout<<"1"<
// else if(n==1)
// cout<// else{
r = n %4;
fastexp(m,ary[r]);
//}
cin>>m;
cin>>n;
}

return 0;
}

0 comments: