ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в ветку

Обсуждение задачи 1209. 1, 10, 100, 1000...

Ответ на сообщение

  • Сообщения должны быть написаны на английском языке и соответствовать тематике сайта.
  • Сообщения не должны содержать оскорблений и нецензурной лексики.
  • Сообщения не должны содержать правильных решений.
Optimal solution
Послано ElPsyCongroo 27 май 2014 22:35
Ok, just posting this to help others understand few things here:

The sequence is 1101001000100000... If you see the locations of the digit 1, you can observe that it is at location 1,2,4,7...=> ( 1 + 0 ),( 1 + 1 ),( 1 + ( 1 + 2 )),( 1+(1+2+3 )..So, it is 1+sum_of_n_digits.

What we have now is below:

1 + n*( n+1 )/2 [ note that n*(n+1)/2 is the sum of first n natural numbers ].

Now, this value should be equal to the input value, say P.

1 + n*(n+1)/2 = p [ I/p P=7 => n should be 3, back track ].

2 + n^2 + n = 2p

n^2 + n = 2(p-1)

n^2 + n - 2 (p-1) = 0;

This is a quadratic equation in 'n'. The roots of a quadratic equation are

( -b +- sqrt( b^2 - 4ac ) )/2a

-1 +- sqrt( 8p-7 ) / 2;

But, for our example, if you do some analysis, if the value of 8p-7 is a perfect square, then the value at that position 'p' would be 1. Else the value would be 0.

So, check for 8p-7 to be a perfect square. This is the optimal solution. Got mine accepted.

BTW, if you face any difficulty at test 3, it means you have to upgrade your ints to doubles. Because 8p-7 can falll over the size of int [atleast in Java].

Thanks,
ElPsyCongroo.


JUDGE_ID
Тема