Advertisement
prakharvk

Golomb sequence

Jun 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int> print(int n){
  4.     if(n==0)
  5.         return {};
  6.     vector<int>ans;
  7.     vector<int>dp(n+1);
  8.     dp[1]=1;
  9.     dp[2]=2;
  10.     int count=0;
  11.     for(int i=1;count<n;i++ ){
  12.         for(int j=0;j<dp[i];j++){
  13.             ans.push_back(i);
  14.             count++;
  15.             dp[count]=i;
  16.         }
  17.     }
  18.     return ans;
  19. }
  20. int main(){
  21.     int n;
  22.     cin>>n;
  23.     vector<int>ans=print(n);
  24.     for(int i=0;i<n;i++){
  25.         cout<<ans[i]<<" ";
  26.     }
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement