Guest User

Untitled

a guest
Oct 24th, 2014
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. // i try to solve this problem ^^ if i wrong, dont laugh me :)
  2. // We easy see that if n = 1 we dont need to cut and ans is 0
  3. // if n = 2 we need 1 cut
  4. // i call a[i] is the number of cut to make cake to i people.
  5. // now if n is a odd number we need 1 cut to make the cake in 2 side (n-1)/n and 1/n
  6. // now we need to share (n-1)/n cake to n-1 people (n-1) now is even. we habe a[n] = a[n-1] + 1;
  7. // if n is a even number, we need 1 cut to make the cake in 2 same side so a[n] = a[n/2] + 1;
  8.  
  9. // It's code
  10.  
  11. #include <cstdio>
  12. #include <iostream>
  13. #include <algorithm>
  14. #include <vector>
  15. #include <cstring>
  16. #include <cmath>
  17.  
  18. using namespace std;
  19.  
  20. int n, m, i, j, a[100009];
  21.  
  22. int main() {
  23.     cin >> n;                       // cin n people
  24.     a[1] = 0;                       // if n = 1 we need 0 cut
  25.     a[2] = 1;                       // if n = 2 we need 1 cut
  26.     for(i = 3; i <= n; i++){        // make a sequence a[i] is the answer of i people
  27.         if(i % 2 == 1){             // if people is odd
  28.             a[i] = a[i - 1] + 1;   
  29.         }  
  30.         else{                       // if people is even
  31.             a[i] = a[i / 2] + 1;
  32.         }
  33.     }
  34.     cout << a[n] << "\n";
  35. }
Advertisement
Add Comment
Please, Sign In to add comment