lightoj1189

求一个数的阶乘进制的表达,同数的二进制表达一样,从上往下贪心。

LL A[23];
void init(){
    A[0] = A[1] = 1LL;
    for (int i = 2;i < 21;++i)
        A[i] = A[i-1]*i;
}
int main()
{    
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    int icase=0,t;
    init();
    cin >> t;
    while(t--){
        LL n;
        cin >> n;
        vector<int> vec;
        for (int i = 20;i >= 0;--i){
            if (n >= A[i]){
                n -= A[i];
                vec.push_back(i);
            }
        }
        printf("Case %d: ",++icase);
        if (n != 0){
            printf("impossible\n");
            continue;
        }
        sort(vec.begin(),vec.end());
        for (int i = 0;i < vec.size();++i){
            if (i != 0) printf("+");
            printf("%d!",vec[i]);
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(数学,lightoj)