HDU1061 Rightmost Digit(快速幂取模)

Rightmost Digit

                                                Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Problem Description
Given a positive integer N, you should output the most right digit of N^N.
 

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).
 

Output
For each test case, you should output the rightmost digit of N^N.
 

Sample Input
  
  
2 3 4
 

Sample Output
  
  
7 6
Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
 

因为进位对个位不影响,积的取余等于取余的积取余

1.如果b是偶数,我们可以记k = a2 mod c,那么求(k)b/2 mod c就可以了。

2.如果b是奇数,我们也可以记k = a2 mod c,那么求

((k)b/2 mod c × a ) mod c =((k)b/2 mod c * a) mod c 就可以了。

 

那么我们可以得到以下算法:

int ans = 1;

a = a % c;

if(b%2==1)

ans = (ans * a) mod c; //如果是奇数,要多求一步,可以提前算到ans

k = (a*a) % c; //我们取a2而不是a

for(int i = 1;i<=b/2;i++)

{

ans = (ans * k) % c;

}

ans = ans % c;

 

我们可以看到,我们把时间复杂度变成了O(b/2).当然,这样子治标不治本。但我们可以看到,当我们令k = (a * a) mod c时,状态已经发生了变化,我们所要求的最终结果即为(k)b/2 mod c而不是原来的ab mod c所以我们发现这个过程是可以迭代下去的。当然,对于奇数的情形会多出一项a mod c,所以为了完成迭代,当b是奇数时,我们通过

ans = (ans * a) % c;来弥补多出来的这一项,此时剩余的部分就可以进行迭代了。

 

形如上式的迭代下去后,当b=0时,所有的因子都已经相乘,算法结束。于是便可以在Olog b的时间内完成了。于是,有了最终的算法:快速幂算法。

int ans = 1;

a = a % c;

while(b>0)

{

if(b % 2 == 1)

ans = (ans * a) % c;

b = b/2;

a = (a * a) % c;

}

将上述的代码结构化,也就是写成函数:

int mypow(int a,int b,int c)
{
    int ans=1;
    a=a%c;
    while(b>0)
    {
        if(b%2==1)
            ans=(ans*a)%c;
        b/=2;
        a=a*a%c;
    }
    return ans;
}


HDU1061.cpp

#include <iostream>
#include <cstdio>
using namespace std;
int mypow(int a,int b,int c)
{
    int ans=1;
    a=a%c;
    while(b>0)
    {
        if(b%2==1)
            ans=(ans*a)%c;
        b/=2;
        a=a*a%c;
    }
    return ans;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("%d\n",mypow(n,n,10));
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值