Problem Description
lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.
Input
There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)
Output
For each test case, you should output the a^b's last digit number.
Sample Input
7 66
8 800
Sample Output
9
6
Yes of cause the upstair solution is OK, but if you want to know why the number like that in array "sam" like that, you can reference my LastBitNumber function.
This function is applied to any modulo not only 10, I've test my program just now, and fix some bug, you can press enter and press controll + Z to end the input. here is the program
#include "stdafx.h"
#include
#include
using namespace std;
// actually it can calculate any modulo last bit, here called this function with modulo = 10
unsigned int Fun_LastBitNumber(unsigned int a, long int exponet, unsigned int modulo)
{
long int i = 0;
unsigned int remainder = 0, temp = 1, remainder_a = 0;
remainder_a = a % modulo;
for (i = 0; i < exponet; i++)
{
temp *= remainder_a;
remainder = temp % modulo;
temp = remainder;
}
return remainder;
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int a = 0, lastbitNumber = 0, modulo = 10;// binary octal hexadecimal are also OK;
long int exponet = 0;
std::vector vecLastBitNum;
std::vector::iterator itervecLastBitNum;
while(cin >> a >> exponet)
{
lastbitNumber = Fun_LastBitNumber(a, exponet, modulo);
vecLastBitNum.push_back(lastbitNumber);
}
//Output
itervecLastBitNum = vecLastBitNum.begin();
while( itervecLastBitNum != vecLastBitNum.end() )
{
cout << *itervecLastBitNum << endl;
itervecLastBitNum++;
}
return 0;
}
You'd better to understand knowledge about polynomial or number theory first.
Here is the program for your reference.
#include "stdafx.h"
#include
#include
using namespace std;
// actually it can calculate any modulo last bit, here called this function with modulo = 10
unsigned int Fun_LastBitNumber(unsigned int a, long int exponet, unsigned int modulo)
{
long int i = 0;
unsigned int remainder = 0, temp = 1, remainder_a = 0;
remainder_a = a % modulo;
for (i = 0; i < exponet; i++)
{
temp *= remainder_a;
remainder = temp % modulo;
temp = remainder;
}
return remainder;
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned int a = 0, lastbitNumber = 0, modulo = 10;// binary octal hexadecimal are also OK;
long int exponet = 0;
std::vector vecLastBitNum;
std::vector::iterator itervecLastBitNum;
while(1)
{
cin >> a;
cin >> exponet;
lastbitNumber = Fun_LastBitNumber(a, exponet, modulo);
vecLastBitNum.push_back(lastbitNumber);
}
//Output
itervecLastBitNum = vecLastBitNum.begin();
while( itervecLastBitNum != vecLastBitNum.end() )
cout << *itervecLastBitNum;
cout << endl;
return 0;
}
you can add any break condition in the while(1) to end of the input,
and obviously the 2 include files are iotream and vector because "<>"can not display