MarioYC

Selectivo 2 - P3

Sep 2nd, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. bool check(int pos, int p, int q, int it){
  8.     if(it == 0 || q < pos || p > pos) return false;
  9.    
  10.     int mi = (p + q) >> 1;
  11.     --it;
  12.    
  13.     if(pos == mi){
  14.         if(it == 0) return true;
  15.         return false;
  16.     }
  17.    
  18.     if(pos < mi) return check(pos,p,mi - 1,it);
  19.     return check(pos,mi + 1,q,it);
  20. }
  21.  
  22. int main(){
  23.     int x,it;
  24.    
  25.     scanf("%d %d",&x,&it);
  26.    
  27.     vector<int> v;
  28.    
  29.     for(int i = 0;i < 10000;++i){
  30.         if(check(x,0,i,it)) v.push_back(i + 1);
  31.     }
  32.    
  33.     int sz = v.size(),ans = 0;
  34.     vector<int> l,r;
  35.    
  36.     for(int i = 0;i < sz;){
  37.         int e = i + 1;
  38.        
  39.         while(e < sz && v[e] - v[i] == e - i) ++e;
  40.        
  41.         l.push_back(v[i]);
  42.         r.push_back(v[e - 1]);
  43.        
  44.         i = e;
  45.         ++ans;
  46.     }
  47.    
  48.     printf("%d\n",ans);
  49.    
  50.     for(int i = 0;i < ans;++i)
  51.         printf("%d %d\n",l[i],r[i]);
  52.    
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment