hdu 1061 Rightmost Digit

来源:互联网 发布:软件配置管理规定 编辑:程序博客网 时间:2024/05/17 23:48


一开始用的是数学公式,后来发现这题要考察的本质应该是快速幂


#include <iostream>
using namespace std;
typedef long long LL;


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        LL n,  result, x;
        cin>>x;
        n=x;
        if(n==0)
        {
            cout<<1<<endl;
            continue;
        }
        else
        {
            while((n&1)==0)
            {
                x*=x;
                x%=10;
                n>>=1;
            }
        }
        result=x;
        result%=10;
        n>>=1;
        while(n!=0)
        {
            x*=x;
            x%=10;
            if(n&1)
            {
                result*=x;
                result%=10;
            }
            n>>=1;
        }
        cout<<result<<endl;
    }
    return 0;
}


































0 0