Complicated

Can anyone help to solve this one?

There is given two numbers n and m (1 ≤ n, m ≤ 10^8);
1) Divide n from 2 until 1.
2) Multiply m from 2 as many times as n was divided.
3) We are getting a table:
e.g. n=69, m=26;

69 26
34 52
17 104
8 208
4 416
2 832
1 1664

4) Delete these rows which numbers in left side are even.
e.g.

69 26
17 104
1 1664

5) Sum these elements which are in right column.
e.g.
26+104+1664=1794

6) cout<<answer.


That's great! I've coded it but somewhere's a mistake. Testing server shows it on 4th test. Any ideas?

#include<iostream>
using namespace std;
int main()
{
long long a,b,p;
cin>>a>>b;
if(a%2!=0)
{
p=p+b;
}
while(a!=1)
{
a=a/2;
b=b*2;
if((a%2)!=0)
{
p=p+b;
}
}
cout<<p;
}
Put it in code tags and people can see what is going on.

You haven't initialised p (as 0).

Since I have no idea what "4th test" is, it's a bit difficult to help any further than that.
It worked. Thanks!
Topic archived. No new replies allowed.