ZOJ 4528 ZOJ Monthly, October 2011 A

  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 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

 

这套题很多数论啊。

这个题目直接暴力即可~

 

原题等价于那个数列的和能够被10^k整除的最大k是多少,枚举就可以了。

但是要用大数。

 

我的代码:

import java.math.*;
import java.util.*;

public class Main {
	static BigInteger power(BigInteger p,BigInteger n,BigInteger m)
	{
		BigInteger sq=BigInteger.ONE;
		while(n.compareTo(BigInteger.ZERO)>0)
		{
			if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE)==0)
				sq=sq.multiply(p).mod(m);
			p=p.multiply(p).mod(m);
			n=n.divide(BigInteger.valueOf(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++)
			mod=mod.multiply(BigInteger.valueOf(10));
		for(i=1;i<=m;i++)
		{
			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);
		}
	}
}


 

你可能感兴趣的:(ZOJ 4528 ZOJ Monthly, October 2011 A)