Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.3 kB
3
Indexable
Never
#include <fstream>

using namespace std;

int f[100005];
int v[100005];
int cif[12];
long long int cnt;
int n, nrcif;
int MinScadere(int a) {
    int m=0, minim=9;
    while(a){
        cif[++m]=a%10;
        a/=10;
        if(cif[m]<minim){
            minim=cif[m];
        }
    }
    int ras=0;
    for(int i=m; i>=1; i--){
        ras=ras*10+(cif[i]-minim);
    }
    return ras;
}

int reverse(int n, int nrcif) {
    int m=0, cn=n, maxim=0;
    for(int i=1; i<=nrcif; i++){
        cif[++m]=cn%10;
        cn/=10;
        maxim=max(maxim, cif[m]);
    }
    int ras=0;
    for(int i=m; i>=1; i--){
        ras=ras*10+(maxim-cif[i]);
    }
    return ras;
}

int nrCifNum(int n) {
    int s=0;
    while (n) {
        s++;
        n/=10;
    }
    return s;
}

int main () {

    ifstream cin("puzzle.in");
    ofstream cout("puzzle.out");

    cin>>n;
    for(int i=1; i<=n; i++){
        cin>>v[i];
        if(i==1){
            nrcif=nrCifNum(v[i]);
        }
        f[MinScadere(v[i])]++;
    }
    cnt=0;
    for(int i=1; i<=100000; i++){
        cnt+=f[i]*1LL*f[reverse(i, nrcif)];
    }
    cnt/=2;
    cnt+=f[0]*1LL*(f[0]-1)/2;

    cout<<cnt<<"\n";

    return 0;
}