Editorial for missing number (Unofficial)

So in this question all you had to do was carefully handle power function and to take care of base conversion
i.e. if string is AB
then the base must be atleast 12

After this all thats left is to store conversion of each number in set and update frequency if, atlast frequency is n then thats the answer else output -1

#include<bits/stdc++.h>
    using namespace std;

    #define mod 1000000000000 
    long long power(long long num,long long expo){
        long long val=1;
        while(expo){
            val*=num;
            if(val>mod || val <0){
                return -1;
                break;
            }
            expo--;
        }
        return val;
    }
    long long converttobase(long long base,string number){
        for(long long j=number.size()-1;j>=0;j--){
            if(int(number[j])>=48 && int(number[j])<=57)
            {
                if(int(number[j])-48>=base)
                    return -1;
            }
            if(int(number[j])>=65 && int(number[j])<=90)
            {
                if(int(number[j])-65+10>=base)
                    return -1;
            }
        }
        long long val=0,poww=0,count=0;
        for(long long j=number.size()-1;j>=0;j--){
            int temp=int(number[j]);
            if(temp>=48 && temp<=57){
                poww=((temp-48))*power(base,count++);
                if(poww<0){
                    // flaggreater=1;
                    return -1;
                }
                
            }
            else{
                poww=(temp-65+10)*power(base,count++);
                if(poww<0){
                    // flaggreater=1;
                    return -1;
                }
            }
            val=val+ poww;
            if(val>mod)
            return -1;
        }
        return val;
    }

    int main(){
        long long t;
        cin>>t;
        while(t--){
            long long n;
            cin>>n;
            long long num=n,ans=-1;
            map<long long,long long>freq;

            while(n--){
                long long index=0;
                long long base;
                string number;
                cin>>base>>number;
                if(base ==-1){
                    set<long long>values;
                    for(long long i=2;i<=36;i++){
                        long long convertedBaseValue=converttobase(i,number);
                        if(convertedBaseValue>=0){
                        values.insert(convertedBaseValue);
                        }
                    }

                    for(auto it=values.begin();it!=values.end();it++){
                        freq[*it]++;
                    }
                }
                else{
                    long long convertedBaseValue=converttobase(base,number);
                                        freq[convertedBaseValue]++;
                }
            }
            for(auto i : freq){
                if(i.second==num && i.first>=0){
                    ans=i.first;
                    break;
                }

            }
            cout<<ans<<endl;
        }
    }
1 Like

This isn’t an editorial, it’s unformatted code.
Edit: if you wanted to post your code, next time include a link to your solution.

This is your solution: here

4 Likes

Pretty effin’ helpful eh?

3 Likes

Sorry for that updated some insights

Thanks @amanrajok , I used set and took intersections each time instead of updating freq in map but still couldn’t figure out about WA. Would appreciate if you can have a look and help.
https://www.codechef.com/viewsolution/27279136

1 Like

Here is my code:
https://www.codechef.com/viewsolution/27360624
This question just want taking care of overflow!!! and thus preventing garbage value to be returned

can anyone tell me what is wrong with my code for #sub-task 1
https://www.codechef.com/viewsolution/27349471

Pre-defined Implementations sometime helpful just we need take care of exceptions
Solution Link
My approach:
generated all possible values for string and finding common element
if not possible printing -1

1 Like

check if the value is crossing 10^12 in cal_ans function only because chances are that
pow(a , (s.length() - 1 - j) might overflow and upon further multiplication it might turn into a positive value and be less than 10^12.
use custom power function so u can break exactly at a point where it crosses 10^12 and return -1.

1 Like

My approach is same as others nothing new…just got the right discuss to post it :stuck_out_tongue:
Here is my solution.

help me in fixing my code

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int LL;
#define mod 1000000000000
LL ME(LL x,LL n)
{
if(n==0) return 1;
LL temp=1;
while(n){
temp*=x;
//if(temp>mod)
//return -1;
n–;
}
return temp;
}

LL fun(int b, int p, int x) {
LL temp= x*ME(b,p);
//if(temp>mod || temp<0) return -1;
return temp;
}
int main() {
ios_base::sync_with_stdio(0),cin.tie(0);
int t, n;
cin>>t;
while(t–) {
cin>>n;
int b; string y; vector < pair < int, string> > v;
unordered_map<LL,int> m;
for(auto i=0;i<n;i++){
cin>>b>>y;
v.emplace_back(make_pair(b,y));
}
int val[26];
for(auto i=0;i<26;i++) val[i] = i + 10 ;
for(auto i=0;i<v.size();i++){
string s1=v[i].second ;
if(v[i].first== -1){
for(auto j=2;j<=36;j++){
bool flag=0;
for(auto g=s1.size()-1;g>=0;g–){
if(s1[g]>=65 && s1[g]<=90){
if(s1[g]-65+10 >=j){flag=1; break;}
}
else{
if(s1[g]-48 >=j){flag=1; break; }
}
}
if(flag == 1) continue;
LL temp2=0; bool flag3=0;
for(auto k=0;k<s1.size();k++){
if( s1[k] >=65 && s1[k] <= 90) {
temp2=(temp2 + fun(j , s1.size()-k-1, val[s1[k] - ‘A’])) ;
}
else{
temp2=(temp2 + fun(j, s1.size()-k-1, s1[k] - 48 )) ; }
if(temp2>mod) {
flag3=1; break;
}
}
if(temp2>=0 && flag3==0)
m[temp2]++;
}
}
else{
bool flag2=0;
for(auto g=s1.size()-1;g>=0;g–){
if(s1[g]>=65 && s1[g]<=90){
if(s1[g]-65+10 >=v[i].first){flag2 =1; break;} }
else{
if(s1[g]-48 >=v[i].first){flag2=1; break; }
}
}
if(flag2==1) continue;
LL temp3=0; bool flag4=0;
for(auto k=0;k<s1.size();k++){
if( s1[k] >=65 && s1[k] <= 90) {
temp3=(temp3 + fun(v[i].first, s1.size()-k-1, val[s1[k] - ‘A’])) ;
}
else {
temp3=(temp3 + fun(v[i].first, s1.size()-k-1, s1[k] - 48 )) ; }
if(temp3>mod) {
flag4=1; break;
}
}
if(temp3>0 && flag4==0)
m[temp3]++;
}
}
std::vector<std::pair<LL, int>> elems(m.begin(), m.end());
std::sort(elems.begin(), elems.end());
bool f=0;
for(auto i=0;i<elems.size();i++){
if(elems[i].second==n && elems[i].first < mod ){
cout<<elems[i].first<<"\n"; f=1;break;
}
}
if(f==0) cout<<"-1\n";
}
return 0;
}

Can u please explain, why u said, for string AB base should be atleast 13?

Because base should be atleast greater than the maximum individual number in that base.

For example:
In base 10(decimal), all number are less than 10 i.e. 0,1,2,3,4,5,6,7,8 and 9.
In base 2(binary), only 0 and 1 are possible which are less than 2.

Now in string AB, we know A=10 and B=11. So possible bases are 12,13,14,15…upto 36.

So, base should be atleast 12.

P.S. - @amanrajok It’s 12 instead of 13.

1 Like

As AB is not possible for base 2,3 or anything before 13
I.e. whatever be the max value in string base would be +1 of that
Example you can’t say 98 is in base 2 it must be a decimal number.

Yes thanks for the correction