ZOJ3549 Little Keng(快速幂)


Little Keng

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Calculate how many 0s at the end of the value below:
1n + 2n + 3n + ... + mn

Input

There are multiple cases. For each cases, the input containing two integers mn. (1 <= m <= 100 , 1 <= n <= 1000000)

Output

One line containing one integer indicatint the number of 0s.

Sample Input

4 3

Sample Output

2


题意:求出1^n+2^n+3^n+……+m^n的值末尾的0的个数。

题目中m和n看起来很大,实际上用快速幂直接计算即可。用sum(long long)表示1^n+2^n+3^n+……+m^n的值,再求出sum末尾0的个数即可。

注意点1:快速幂取模,模值为1000000000(10^9)(估计答案最大不会大于9个0)
注意点2:sum求和过程中不要取模,否则会WA(应该有组数据正好有9个0,取模会导致sum为0)
注意点3:非递归快速幂时需要long long,不然会溢出

#include <iostream>
#include <cstdio>
using namespace std;

const int mod=1000000000;

int n,m;

long long Qmod(int x,int b)
{
     long long res=1,a=x; //注意此处用long long来代替了底数,避免在a*a时溢出
     while (b)
     {
           if (b&1) res=res*a%mod;
           a=a*a%mod;
           b>>=1;
     }
     return res;
}

int main()
{
    while (scanf("%d%d",&m,&n)!=EOF)
    {
          long long sum=0;
          for (int i=1;i<=m;i++)
          {
              sum=sum+Qmod(i,n); //此处不要加上%mod,否则会WA
          }
          int ans=0;
          while (sum%10==0&&sum>0)
          {
                ++ans;
                sum/=10;
          }
          printf("%d\n",ans);
    }
    return 0;
}
     


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值