P1591 阶乘数码

题目传送门

#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 10;
int a[N];

void mul(int a[], int b, int &len) {
    int t = 0;
    for (int i = 1; i <= len; i++) {
        t += a[i] * b;
        a[i] = t % 10;
        t /= 10;
    }
    while (t) {
        a[++len] = t % 10;
        t /= 10;
    }
    //去前导0
    while (len > 1 && !a[len]) len--;
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        int n, x;
        scanf("%d %d", &n, &x);
        int al = 0, cnt = 0;
        a[++al] = 1;
        for (int i = 2; i <= n; i++) mul(a, i, al);

        for (int i = 1; i <= al; i++)
            if (a[i] == x) cnt++;
        printf("%d\n", cnt);
    }
    return 0;
}
posted @ 2021-11-22 15:51  糖豆爸爸  阅读(82)  评论(0编辑  收藏  举报
Live2D