Winter Racing in Informatics
Veliko Tarnovo, 27 February-1 March 2015
Group D, 6 – 7 Grade
Task D1. Binary numbers
Author: Velislava Damodaran
Joanna found it all the way to the autumn competition (for her immense regret) that the numbers
Can be recorded in other number systems, and that not always 2 and 2 makes 4! She became
Curious and in a boring English class decided to write as many as possible
Sequential numbers in the binary number system, starting with the number one. But because
There was not much room on the sheet (however, the words from the English language), it
He wrote the numbers side by side without putting any spaces between them. So you get a long
A number of zeros and units: 110111001011101111000100110101011110011011110...
At recess showed this creation of his friend Irina, but she could not
Find out what number is written. Joanna got angry and said that any good programmer could
Find out which number in the decimal number system is written on the sheet, as long as it knows in
The resulting number of units and zeros the position of one digit in the binary number
System. Since Joanna is already in the eighth grade and this task is very easy for her, she wants you to
Write a program called binary to help Irina.
Login
From the first line of the standard input, the position of one digit per number is entered
Binary number system in the resulting series of zeros and units
Output
On one line of the standard output, the program must display two numbers separated by
Just one space.
The first number must be the number in the decimal number system whose number is
Binary number system is written in the specified position.
The second number must be the digit in the binary number system that stands on the given
Position.
Restrictions
0 < 1016
Example
Log out
25 9 1
---------------------------------------------------------------
My solve:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
long long pos,n,sum,k,ans,ost;
int main() {
cin>>pos;
for (n=1; n++;) {
sum+=(1ll<<(n-1))*n;
if (sum>=pos) break;
}
sum-=(1ll<<(n-1))*n;
pos-=sum;
k=(pos-1)/n+1;
ans=(1ll<<(n-1))+(k-1);
cout<<ans-1<<" ";
ost=pos%n;
if (ost==0) ost=n;
pos=n+1-ost;
for (long long i=1; i<pos; i++) {
ans/=2;
}
cout<<ans%2<<endl;
return 0;
}
But I don't understand why I must subtract 1 from ans. If I don't my answer is 10.