题目描述
求 n!n! 中某个数码出现的次数。
输入格式
第一行为 t (t≤10),表示数据组数。接下来 t 行,每行一个正整数 n (n≤1000) 和数码 a。
输出格式
对于每组数据,输出一个整数,表示 n!n! 中 aa 出现的次数。
输入输出样例
输入:
2
5 2
7 0
输出:
1
2
code:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int maxn = 30000;
int t, n, a;
int main()
{
cin >> t;
while (t--)
{
scanf("%d %d", &n, &a);
int f[maxn];//结果数组
memset(f, 0, sizeof(f));
f[0] = 1;
for (int i = 2; i <= n; i++)
{
int c = 0;//进位储存器
for (int j = 0; j < maxn; j++)
{
int s = f[j] * i + c;
f[j] = s % 10;
c = s / 10;
}
}
int k;
for (int i = maxn - 1; i >= 0; i--)
{//去除前导0
if (f[i])
{
k = i;
break;
}
}
int count = 0;
for(int i = k;i >= 0;i--){
if(f[i] == a){
count++;
}
}
cout << count << endl;
}
}