A-Again Twenty Five!

本文介绍了如何使用快速幂取模算法解决计算5的n次方最后两位数字的问题,强调了算法效率和逻辑实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Again Twenty Five!
time limit per test0.5 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard output
The HR manager was disappointed again. The last applicant failed the interview the same way as 24 previous ones. “Do I give such a hard task?” — the HR manager thought. “Just raise number 5 to the power of n and get last two digits of the number. Yes, of course, n can be rather big, and one cannot find the power using a calculator, but we need people who are able to think, not just follow the instructions.”

Could you pass the interview in the machine vision company in IT City?

Input
The only line of the input contains a single integer n (2 ≤ n ≤ 2·1018) — the power in which you need to raise number 5.

Output
Output the last two digits of 5n without spaces between them.

Examples
input
2
output
25

题意:求出5的n次方,然后输出最后两个数。
PS:只要n大于0,不管怎么平方,输出的最后两位都是25。。。。
当然,鉴于前面有那么多人做错,我决定还是安安静静的用快速幂求模。

直接上代码

#include<cstdio>
#include<stack>
#include<cstring>
#include<cctype>
#include<cstdlib>
using namespace std;
int pow(int a,long long int n,int b)//快速幂取模
{
    int result=1;
    a=a%b;//积的取余等于取余的积取余
    while(n>0)
    {
        if(n%2==1)
            result=result*a%b;
        n=n/2;
        a=a*a%b;
    }
    return result;
}
int main()
{
    long long int n;
    while(scanf("%lld",&n)!=EOF)
    {
        printf("%d\n",pow(5,n,100));
    }
    return 0;
}

关于快速幂取模问题,我之前有篇文章特意总结过
http://blog.csdn.net/qq_32680617/article/details/50640622

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值