0分求助

P1591 阶乘数码

telankesi @ 2023-01-16 09:43:31



#include<stdio.h>
long long fun(int a, int b) {
    int f[10000] = { 0 };
    f[1] = 1;
    int len = 1;
    int t = 1;
    while (t <= a) 
    {
        for (int i = 1; i <= len; i++) {
            f[i] *= t;
            if (f[i] >= 10) {
                f[i + 1] += f[i] / 10;
                f[i] %= 10;
                if (i == len) {
                    len++;
                    break;
                }
            }

        }t++;
    }
    long long num = 0;
    for (int i = 1; i <= len; i++) {
        if (f[i] == b)num++;
    }
    return num;
}
int main()
{    
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        printf("%lld\n", fun(a, b));
    }

    return 0;
}

|