题意是说给出两个整数 m,n,然后有sum = 1n + 2n + 3n + ... + mn
问sum末尾有几个0,。
看人家报告的思路:问题等价与sum能被 10^k 整除,k 值是多少,快速幂求出 sum,然后枚举 k 的值即可。
不过中间 sum 值会很大,用 java 写着方便一点,代码来自
http://blog.csdn.net/xieshimao/article/details/6917827
代码:
import java.math.*;
import java.util.*;
public class fucking {
static BigInteger power(BigInteger p,BigInteger n,BigInteger m) //快速幂
{
BigInteger sq=BigInteger.ONE;
while(n.compareTo(BigInteger.ZERO)>0) //while(n>0)
{
if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE)==0) //if(n%2==1)
sq=sq.multiply(p).mod(m); // sq=(sq*p)%m;
p=p.multiply(p).mod(m); //p=(p*p)%m;
n=n.divide(BigInteger.valueOf(2)); //n=n/2;
}
return sq.mod(m);
}
static boolean judge(int m,int n,int k)
{
BigInteger mod=BigInteger.ONE,ans=BigInteger.ZERO;
int i;
for(i=1;i<=k;i++) //10^k
mod=mod.multiply(BigInteger.valueOf(10));
for(i=1;i<=m;i++) //1^n+2^n+3^n+...+m^n
{
BigInteger a,b;
a=BigInteger.valueOf(i);
b=BigInteger.valueOf(n);
ans=(ans.add(power(a,b,mod))).mod(mod);
}
if(ans.mod(mod).compareTo(BigInteger.ZERO)==0) return true;
else return false;
}
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
int i,m,n;
while(cin.hasNext())
{
m=cin.nextInt();
n=cin.nextInt();
for(i=1;;i++)
{
if(judge(m,n,i)) continue;
else break;
}
System.out.println(i-1);
}
}
}