Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.4 kB
1
Indexable
Never
#include <bits/stdc++.h>

using namespace std;

ifstream fin("cat2pal.in");
ofstream fout("cat2pal.out");
const int NMAX = 1e5 + 5;
bool f[NMAX];
long long int c, n, a, cnt, nrcif_a, p=1, nr, nr2, nr3, ans, f_pal0[NMAX], f_pal1[NMAX];
vector<long long int> v, s;
vector<int> cifre;

bool isPalindrom(long long int x){
    long long int ras=0, cx=x;
    while(x>=1){
        ras=ras*10+x%10;
        x/=10;
    }
    if(ras==cx) return true;
    return false;
}

long long int rasturnat(long long int x){
    long long int ras=0;
    while(x>=1){
        ras=ras*10+x%10;
        x/=10;
    }
    return ras;
}

int nrcif(long long int x){
    long long int cnt=0;
    if(x == 0)
        return 1;
    while(x>=1){
        cnt++;
        x/=10;
    }
    return cnt;
}

long long int concat1(long long int x, long long int y){
    long long int conct=y;
    conct=pow(10, nrcif(y))*x+y;
    return conct;
}

long long int prefix(long long int pal)
{
    /// 123321 -> 123, 12321 -> 123
    /// 6 cifre-> elimin 3 cif, 5 cif-> elimin 2 cif
    int nrc = nrcif(pal)/2;
    long long int p = pow(10, nrc);
    return pal / p;
}


void solve(){
    fin>>c;
    if(c==1){
        int i=0, j=0;
        fin>>a;
        nr=rasturnat(a);
        nr2=nr;
        nr3=nr;
        nrcif_a=nrcif(a);
        s.push_back(nr);
        p=pow(10, nrcif_a-1);
        while(p>=1){
            if(isPalindrom(concat1(a, nr))){
                for(i=0; i<s.size(); i++)
                    if(s[i]==nr) break;
                if(i==s.size()){
                    s.push_back(nr);
                }
            }
            if(isPalindrom(concat1(nr2, a))){
                for(i=0; i<s.size(); i++)
                    if(s[i]==nr2) break;
                if(i==s.size()){
                    s.push_back(nr2);
                }
            }
            nr2/=10;
            nr=nr%p;
            p/=10;
        }
        for(int i=1; i<=9; i++){
            long long int aux=a;
            long long int x=i;
            while(aux){
                x=x*10+aux%10;
                aux/=10;
            }
            if(x<=a*10){
                s.push_back(x);
            }
        }
        if(a%10!=0){
            for(int i=0; i<=9; i++){
            long long int x=concat1(nr3, i);
            if(x<=a*10){
                for(j=0; j<s.size(); j++)
                    if(x==s[i]) break;
                if(j==s.size()){
                    s.push_back(x);
                }
            }
        }
    }
    fout<<s.size();
}
    if(c==2){
        ans = 0;
        fin>>n;
        for(int i=0; i<n; i++){
            fin>>a;
            v.push_back(a);
            f[a]= 1;
        }
        for(int i=0; i<n; i++){
            long long int ras = rasturnat(v[i]);
            long long int nr1 = ras;
            long long int nr2 = ras;

            p = pow(10, nrcif(ras) - 1);

            while(nr1)
            {
                ///123 - 321 - nr
                long long int pal1 = concat1(nr1, v[i]);
                long long int pal2 = concat1(v[i], nr2);
                if(isPalindrom(pal1) && f[nr1]){
                    long long int pal1_prefix = prefix(pal1);
                    if(nrcif(pal1)%2 == 0 && !f_pal0[pal1_prefix])
                    {
                        ans++;
                        f_pal0[pal1_prefix] = 1;
                    }
                    if(nrcif(pal1)%2 == 1 && !f_pal1[pal1_prefix])
                    {
                        ans++;
                        f_pal1[pal1_prefix] = 1;
                    }
                }
                if(isPalindrom(pal2) && f[nr2]){
                    long long int pal2_prefix = prefix(pal2);
                    if(nrcif(pal2)%2 == 0 && !f_pal0[pal2_prefix])
                    {
                        ans++;
                        f_pal0[pal2_prefix] = 1;
                    }
                    if(nrcif(pal2)%2 == 1 && !f_pal1[pal2_prefix])
                    {
                        ans++;
                        f_pal1[pal2_prefix] = 1;
                    }
                }

                nr1 /= 10;
                nr2 = nr2 % p;
                p = p / 10;
            }
        }
        fout<<ans;
    }
}

int main(){
    solve();
    return 0;
}