Problem 1097

本文介绍了一种解决特定数学问题的算法:给定两个正整数a和b,如何高效地计算a^b的最后一位数字。通过使用余数的概念并限制计算范围来优化算法,避免了直接计算大数值的复杂性和资源消耗。
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

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->#include <iostream>

using namespace std;

int main()
{
    
int a, b, result = 1, temp, has = 0, pos, cap = 0;
    
int yushu[10];

    
while (cin >> a >> b)
    {
        temp 
= b;
        has 
= 0;
        pos 
= b;
        cap 
= 0;
        
for(int i = 0; i < 10; i++)
        {
            yushu[i] 
= 0;
        }
        
while(a > 9)
        {
            a 
%= 10;
        }
        
while(temp--)
        {
            result 
*= a;
            
while(result > 9)
            {
                result 
%= 10;
            }
            
for(int i = 0; i < 10; i++)
            {
                
if(yushu[i] == result)
                {
                    has 
= 1;
                    
break;
                }
            }
            
if(has == 1)
                
break;
            
else
            {
                yushu[cap] 
= result;
                cap
++;
            }
        }
        
if(has == 1 && cap != 0)
        {
            pos 
= b % (cap) - 1;
            
if(pos == -1)
                pos 
= cap - 1;
            cout 
<< yushu[pos] << endl;
        }
        
else
            
if(has != 1)
                cout 
<< result << endl;
        
if(has == 1 && cap == 0)
            cout 
<< 0 << endl;
        result 
= 1;
        a 
= 0;
        b 
= 0;
        
for(int i = 0; i < 10; i++)
        {
            yushu[i] 
= 0;
        }
    }
    
return 0;
}

C++语言画矩形 "_AFXDLL" "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\Test.rc"" Creating temporary file "C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8A.tmp" with contents [ /nologo /MDd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /Fp"Debug/Test.pch" /Yu"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ /c "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\MainFrm.cpp" "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\Picdlg.cpp" "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\Test.cpp" "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\TestDoc.cpp" "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\TestView.cpp" ] Creating command line "cl.exe @C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8A.tmp" Creating temporary file "C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8B.tmp" with contents [ /nologo /MDd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /Fp"Debug/Test.pch" /Yc"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ /c "E:\E03教学\2011下半年\图形学\计算机图形学基础教程(Visual C++版)\第五章\案例9-二维基本几何变换算法\StdAfx.cpp" ] Creating command line "cl.exe @C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8B.tmp" Creating temporary file "C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8C.tmp" with contents [ /nologo /subsystem:windows /incremental:yes /pdb:"Debug/Test.pdb" /debug /machine:I386 /out:"Debug/Test.exe" /pdbtype:sept ".\Debug\MainFrm.obj" ".\Debug\Picdlg.obj" ".\Debug\StdAfx.obj" ".\Debug\Test.obj" ".\Debug\TestDoc.obj" ".\Debug\TestView.obj" ".\Debug\Test.res" ] Creating command line "link.exe @C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\RSP8C.tmp" <h3>Output Window</h3> Compiling resources... Compiling... StdAfx.cpp Compiling... MainFrm.cpp Picdlg.cpp Test.cpp TestDoc.cpp TestView.cpp Generating Code... Linking...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值