zoj Little Keng(快速幂)

 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;

 

posted @ 2015-08-22 16:39  Lincy*_*  阅读(182)  评论(0编辑  收藏  举报