zoj Little Keng(快速幂)

本文介绍了一种计算特定数学序列末尾0数量的方法。通过使用C++编程语言实现快速幂算法,解决了一系列数学问题。文章包含源代码示例及算法解析。

 A. Little Keng
Time Limit: 2000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main
Submit Status

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 m, n. (1 <= m <= 100 , 1 <= n <= 1000000)


Output

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


Sample Input

4 3


Sample Output

2

思路:用到了大数定义long long ,快速幂,,直接用模版

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define mod 1000000000
int m,n;
//long long sum;
long long ksm(int q,int n)
{
    long long s=1,t=q;
    while(n>0)
    {
        if(n%2!=0)
        {
            n=n-1;
            s=s*t%mod;
        }
        n=n/2;
        t=t*t%mod;
    }
    return s;
}
int main()
{

    while(~scanf("%d%d",&m,&n))
    {
        int t=0;
        long long sum=0;
        for(int i=1;i<=m;i++)
        {
            sum+=ksm(i,n);
        }
     while(sum>0)
     {
         if(sum%10==0)
         {
             t++;
         }
         else
         break;
         sum=sum/10;
     }
     cout<<t<<endl;
    }
     //cout<<ksm(m,n)<<endl;

 

转载于:https://www.cnblogs.com/dshn/p/4750539.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值