sainivikas's blog

By sainivikas, history, 10 years ago, In English

I am trying to solve problem Hacking Cypher

Hacking Cypher

I am getting wrong answer on test case 50. The test case is not fully visible and i am unable to find my bug.Please help.

This is my solution,

my Solution

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

int main()
{
    string s;
    long long a,b,l=10;
    long long len,i,j;
    cin>>s;
    cin>>a>>b;
    len = s.size();
    long long p_rem[len],s_rem[len];
    p_rem[0]=(s[0]-'0')%a;
    s_rem[len-1]=(s[len-1]-'0')%b;
    for(i=1;i<len;i++)
    {
        p_rem[i]=(p_rem[i-1]*10+(s[i]-'0'))%a;
    }
    for(i=len-2;i>=0;i--)
    {
        s_rem[i]=((s[i]-'0')*l+s_rem[i+1])%b;
        l=l*10;
    }
    for(i=0;i<len-1;i++)
    {
        if(p_rem[i]==0 && s_rem[i+1]==0 && s[i+1]!='0')
            break;
    }
    if(i<len-1)
    {
        cout<<"YES"<<endl;
        for(j=0;j<=i;j++)
            cout<<s[j];
        cout<<endl;
        for(j=i+1;j<len;j++)
            cout<<s[j];
    }
    else
        cout<<"NO";
    return 0;
}
  • Vote: I like it
  • -2
  • Vote: I do not like it

»
10 years ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

it fails due to overflow of l. This is the correction.

 l=(l*10)%b;