Sunday, 13 November 2016

Nuclear Reactors

Problem Statement

There are K nuclear reactor chambers labelled from 0 to K-1. Particles are bombarded onto chamber 0. The particles keep collecting in the chamber 0. However if at any time, there are more than N particles in a chamber, a reaction will cause 1 particle to move to the immediate next chamber(if current chamber is 0, then to chamber number 1), and all the particles in the current chamber will be be destroyed and same continues till no chamber has number of particles greater than N. Given K,N and the total number of particles bombarded (A), find the final distribution of particles in the K chambers. Particles are bombarded one at a time. After one particle is bombarded, the set of reactions, as described, take place. After all reactions are over, the next particle is bombarded. If a particle is going out from the last chamber, it has nowhere to go and is lost.

Input

The input will consist of one line containing three numbers A,N and K separated by spaces. A will be between 0 and 1000000000 inclusive. N will be between 0 and 100 inclusive. K will be between 1 and 100 inclusive. All chambers start off with zero particles initially.

Output

Consists of K numbers on one line followed by a newline. The first number is the number of particles in chamber 0, the second number is the number of particles in chamber 1 and so on.

Example

Input:
3 1 3
Output:
1 1 0
Explanation Total of 3 particles are bombarded. After particle 1 is bombarded, the chambers have particle distribution as "1 0 0". After second particle is bombarded, number of particles in chamber 0 becomes 2 which is greater than 1. So, num of particles in chamber 0 becomes 0 and in chamber 1 becomes 1. So now distribution is "0 1 0". After the 3rd particle is bombarded, chamber 0 gets 1 particle and so distribution is "1 1 0" after all particles are bombarded one by one.

Problem Link: Reactors

The idea:    

This question requires to convert a number in to its (N+1)-ary format upside till k number of digits. For example for the number A=3 and N=1 and K=3  , the binary representation would be 11 . But if you print it upside down till 3 digits it is  110. 

The Code:

  1. #include<iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. int main()
  5. {
  6. long long int A;
  7. int N,K;
  8. scanf("%lld %d %d",&A,&N,&K);
  9. int div=N+1;
  10. while( K)
  11. {
  12. int rem=(A%div);
  13. A=A/div;
  14. cout<<rem<<" ";
  15. K--;
  16. }
  17. cout<<endl;
  18. return 0;
}

No comments:

Post a Comment