为什么会TLE?

P1591 阶乘数码

FishingStar @ 2020-11-29 18:38:24

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
struct BIG{
    int a[10005]; // digi 
    int n; // num
/*    
    int get_value(int i) {
        if (i < n)
            return a[i];
        else
            return 0;
    }

    operator [] {
        if (i < n)
            return a[i];
        else
            return 0;
    }*/
};
/*
BIG a;
a[i];
BIG.get_value(i)
*/
BIG dec(BIG c){
    int t = c.n;
    t--;
    while(t > 0 && c.a[t] == 0){
        t--;
    }
    c.n = t + 1;
    return c;
}

BIG z(){
    BIG a;
    a.n = 1;
    return a;
}

BIG Change(int a){
    BIG cnt;
    int w = 0;
    int t = a;
    while(t > 0){
        cnt.a[w] = t % 10;
        w++;
        t /= 10;
    }
    cnt.n = w;
    return cnt;
}

BIG Cin(){
    BIG cnt;
    int w[10005];
    char ch;    
    cin >> ch;
    int t = 0;
    while(ch >= '0' && ch <= '9'){
        w[t] = ch - '0';
        t++;
        ch = getchar();
    } 
    for(int i = 0; i < t; i++){
        cnt.a[t - i - 1] = w[i];
    }
    cnt.n = t;
    return cnt;
}

void Cout(BIG c){
    dec(c);
    int t = c.n;
    for(int i = t - 1; i >= 0; i--){
       cout << c.a[i];
    }
    cout << endl;
}

BIG add(BIG A, BIG B){
    BIG c;
    int na = A.n;
    int nb = B.n;
    int t = 0;
    int ten = 0;
    while(ten > 0 || t < na || t < nb){
    //  cout << t << endl;
        int ax, bx;
        ax = t < na ? A.a[t] : 0;
        bx = t < nb ? B.a[t] : 0;
        c.a[t] = ax + bx + ten;
        ten = c.a[t] / 10;
        c.a[t] %= 10;
        t++;
    }
    t--;
    while(t > 0 && c.a[t] == 0){
        t--;
    }
    t++;
    c.n = t;
    return c;
}

BIG cf1(int s, int q, BIG B) {
    int t = 0;
    int ten = 0;
    BIG ans = z();
    for(int i = 0; i < 10005; i++){
        ans.a[i] = 0;
    }
    int nb = B.n;
    while(t < nb || ten != 0) {
        ans.a[t + q] = s * ((t < nb) ? B.a[t] : 0) + ten;
        ten = ans.a[t + q] / 10;
        ans.a[t + q] -= ten * 10;
        t++;
    }
    ans.n = t + q;
    return ans;
}

BIG cf(BIG A, BIG B){
    int na = A.n;
    int nb = B.n;
    BIG ans = Change(0);
    for(int i = 0; i < na; i++){
        ans = add(cf1(A.a[i], i, B), ans);
    }
    return ans;
}
int main(){
int T;
cin >> T;
while(T--){
    int n, x;
    cin >> n >> x;
    BIG q = Change(1);
    BIG w = q;
    BIG w1 = w;
    for(int i = 1; i <= n; i++){
        cout << i << endl;
        BIG w = add(w, w1);
        q = cf(q, w);
    }
    q = dec(q);
    int ans = 0;
    for(int i = q.n - 1; i >= 0; i--){
        ans += (q.a[i] == x);
    }
    cout << ans << endl;
}
    return 0;
}

|