raymonds @ 2022-08-01 15:53:25
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int res[3000],sav[3000],num[4],len,count[10];
void _transfer(int n,int* num) //整型数转高精数组
{
memset(num,0,sizeof(num));
char tmp[5]={0};
int i,j;
sprintf(tmp,"%d",n);
for(i=strlen(tmp)-1,j=0;i>=0;i--,j++)
num[j]=int(tmp[i]-48);
}
void multi()
{
memset(sav,0,sizeof(sav));
for(int i=0;i<len;i++)
{
for(int j=0;j<4;j++)
{
sav[i+j]+=num[j]*res[i];
}
}
for(int i=0;i<len+4;i++)
{
sav[i+1]+=sav[i]/10;
sav[i]=sav[i]%10;
res[i]=sav[i];
}
int z=2999;
while(!res[z])
z--;
len=z+1;
}
void jiecheng(int n) //计算n的阶乘
{
memset(res,0,sizeof(res));
len=1,res[0]=1;
for(int i=2;i<=n;i++)
{
_transfer(i,num);
multi();
}
}
int _counter(int n,int num) //在n!结果中寻找字码num并返回出现次数
{
jiecheng(n);
int counter=0;
for(int i=0;i<len;i++)
{
if(res[i]==num)
counter++;
}
return counter;
}
int main()
{
int n,N,zima,i=0;
cin>>n;
while(n)
{
cin>>N>>zima;
cout<<_counter(N,zima)<<endl;
n--;
}
return 0;
}