洛谷-P1591 阶乘数码

洛谷-P1591 阶乘数码

原题链接:https://www.luogu.com.cn/problem/P1591


题目描述

(n!) 中某个数码出现的次数。

输入格式

第一行为 (t(t leq 10)),表示数据组数。接下来 (t) 行,每行一个正整数 (n(n leq 1000)) 和数码 (a)

输出格式

对于每组数据,输出一个整数,表示 (n!)(a) 出现的次数。

输入输出样例

输入 #1

2
5 2
7 0

输出 #1

1
2

C++代码

#include <iostream>
#include <cstring>
using namespace std;

int f[5001];

void mmultip(int a[], int c) {
    int jw = 0;
    for (int i=1; i<=5000; ++i) {
        a[i] = a[i] * c + jw;
        jw = a[i] /10;
        a[i] %= 10;
    }
}

int countA (int n, int a) {
    int i, count = 0;
    memset(f, 0, sizeof(f));
    f[1] = 1;
    for (i=2; i<=n; ++i)
        mmultip(f, i);
    for (i=5000; f[i] == 0; --i);
    for (; i>=1; --i)
        if (f[i] == a)
            ++count;
    return count;
}

int main() {
    int t;
    cin >> t;
    int n[t], a[t], ans[t];
    for (int i=0; i<t; ++i)
        cin >> n[i] >> a[i];
    for (int i=0; i<t; ++i)
        ans[i] = countA(n[i], a[i]);
    for (int i=0; i<t; ++i)
        cout << ans[i] << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/yuzec/p/13450665.html