ACM (TJU) : 2569


// http://acm.tju.edu.cn/toj/showp2569.html

package Vol_XVI;

import java.math.BigInteger;
import java.util.Scanner;

/**
 * @author Md. Shahab Uddin
 * Id : 014081002
 * Dept : CTE, UIU
 */
public class _2569 {

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);

        int testcases = input.nextInt();

        while ( testcases-- != 0 ) {
            int n = input.nextInt();
            int digit = input.nextInt();
            char d = (char) (digit + 48);

            BigInteger factorial = BigInteger.ONE;
            BigInteger one = BigInteger.ONE;
            BigInteger increment = BigInteger.ONE;

            for ( int i = 1; i <= n; i++ ) {
                factorial = factorial.multiply(increment);
                increment = increment.add(one);
            }

            String str = factorial.toString();
            //System.out.println (str);

            int count = 0;
            for ( int i = 0; i < str.length(); i++ ) {
                if ( str.charAt(i) == d )
                    count++;
            }

            System.out.println (count);
        } 
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s