ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1537. Ents

WA#8
Posted by Duzhy Igor 5 Mar 2007 12:03
I think, it's a simple recursion.
If N is even, then
f(N)=f(N-1)+f(N/2)
else if N is odd, then
f(N)=f(N-1)
Am I right?
Re: WA#8
Posted by svr 5 Mar 2007 13:41
mod P also important espacially if P==1
Re: WA#8
Posted by Duzhy Igor 5 Mar 2007 14:58
I count it at the end before the output.
There is my code:
#include <iostream>
using namespace std;

__int64 FindAnswer(const __int64 &num);

int main(void)
{
   // Input data
   __int64 K, P;
   cin >> K >> P;
   // Outputs the result
   cout << (FindAnswer(K)%P) << endl;
   return 0;
}
// Function finds the answer
__int64 FindAnswer(const __int64 &num)
{
   if(num==2)
      return 1;
   if(num<2)
      return 0;
   if(num%2)
      return num/2;
   return FindAnswer(num-1) + FindAnswer(num/2);
}
Re: WA#8
Posted by svr 5 Mar 2007 16:18
It should to combine recursion and Dynamic prog.
At the beggining build array F1[10000000] using formula and
from K>10000000 go by recursion not to 1 as you
but only to first value in [1..10000000]
But %P we must make in FindAnswer befor return
or we will have overflow and i don't understand
conditiom if (num%2)
must be if (num%2==0)
Re: WA#8
Posted by KIRILL(ArcSTU) 5 Mar 2007 16:50
To Duzhy Igor
Your solution has non polynomial complexity

Why you are using this way
Just fill array[2..10000000] from 2 to k using your formula
at the end out (mas[k]%p)

Edited by author 05.03.2007 17:00
Re: WA#8
Posted by Duzhy Igor 7 Mar 2007 12:39
Thank you very much, I have solved it. Good luck!!!