i was trying to do this problem but i could not able to get it accepted , its input is large so i thought to take it with strings but still i am getting wrong answer .
problem statement is here BISHOPS
here is my code:
#include<iostream>
#include<string.h>
#include<cstdio>
#include<vector>
using namespace std;
int main()
{
string s;
int z;
cin>>s;
z = s.length();
int i;
vector<int>v;
int arr[z];
for(i=0;i<z;i++)
{
arr[i] = (s[i]-'0');
}
if ((arr[0] == 0 || arr[0] == 1) && z == 1)
{
printf("%d",arr[0]);
}
else
{
int carry = 0;
for(i=z-1;i>=0;i--)
{
int x = (carry + 2*arr[i])%10;
v.push_back(x);
carry = ( 2*arr[i] )/10;
}
if(carry > 0)
{
v.push_back(carry);
}
int t = v.size();
int g=0;
if(v[g] >=2)
{
v[g] = v[g]-2;
}
else
{
v[g] = 8;
g++;
while(v[g] == 0)
{
v[g] = 9;
g++;
}
v[g] = v[g] -1;
}
if(v[t-1] == 0)
{
for(i=t-2;i>=0;i--)
{
printf("%d",v[i]);
}
}
else
{
for(i=t-1;i>=0;i--)
{
printf("%d",v[i]);
}
}
}
return 0;
}
question is to find max non attackable bishops in the problem for nxn chess board ,i think i am doing right because except for 1×1 for every oyher chess board max non attacakable bishops will be 2*n-2.but still spoj is giving wrong answer
You have integer overflow for all inputs larger than
INT_MAX:You are trying to convert the input to an
int, which typically is a signed 32-bit type withINT_MAXequal to 2147483647 = 231-1. Whenever the input is larger than that, at the tenth digit you have overflow (which, by the way, is undefined behaviour).You need a way to represent/handle numbers larger than that, up to 10100. No standard integer type in C or C++ is wide enough to handle that. You have to roll your own big-integer type (since SPOJ presumably doesn’t link the code with GMP). For this problem, something very simple works well enough.