Guest User

Untitled

a guest
May 25th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <new>
  3. #include <cmath>
  4. #include <exception>
  5. #include <stdexcept>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <tuple>
  9. #include <utility>
  10. #include <chrono>
  11. #include <sstream>
  12. #include <fstream>
  13. #include <iterator>
  14. #include <string>
  15. #include <cassert>
  16. #include <algorithm>
  17.  
  18. uint64_t index_univ = 0;
  19.  
  20. std::vector<std::string> vec = {"T","F"};
  21.  
  22. std::vector<std::vector<std::string>> vecGrp(10,vec);
  23. uint64_t index = 1;
  24.  
  25.  
  26.  
  27. template <typename T>
  28. std::string toStr(T toChange)
  29. {
  30. std::stringstream ss;
  31. ss << toChange;
  32. std::string retStr = ss.str();
  33. return retStr;
  34. }
  35.  
  36. std::string create_binary(uint64_t n)
  37. {
  38. std::string retStr;
  39. while(n!=0)
  40. {
  41. uint64_t bin = n%2;
  42. auto ins = toStr<uint64_t>(bin);
  43. retStr += ins;
  44. n/=2;
  45. }
  46. reverse(retStr.begin(),retStr.end());
  47. return retStr;
  48. }
  49.  
  50. bool createBinIndex(uint64_t index)
  51. {
  52.  
  53. const auto &vecB = vecGrp[index-2];
  54.  
  55. auto &vec = vecGrp[index-1];
  56. vec = vecB;
  57. uint64_t k_ = vec.size();
  58. try{
  59. vec.reserve(vec.size()^2);
  60. }
  61. catch(...)
  62. {
  63. std::cerr << "err1";
  64. throw std::bad_alloc();
  65. }
  66.  
  67. uint64_t i=1;
  68.  
  69. while(i<k_)
  70. {
  71. try
  72. {
  73. copy(vecB.begin(),vecB.end(),std::back_inserter(vec));
  74. }
  75. catch(...)
  76. {
  77. std::cerr << "err2";
  78. throw std::bad_alloc();
  79. }
  80. i++;
  81. }
  82.  
  83. i=0;
  84.  
  85. for(auto &elem : vecB)
  86. {
  87. //std::cout << i << " ";
  88. for(uint64_t k=0;k<k_;k++)
  89. {
  90. vec[i]+=elem;
  91. i++;
  92. }
  93. }
  94.  
  95. return true;
  96. }
  97.  
  98. bool updateIndex(uint64_t check)
  99. {
  100. while(index<check)
  101. {
  102. ++index;
  103.  
  104. try{
  105. createBinIndex(index);
  106. }
  107. catch(const std::exception& e)
  108. {
  109. std::cerr<<"nCaught Exception in "<<__FILE__<<" nat line "
  110. << __LINE__ << " in " << __func__ << " as n" << e.what();
  111. }
  112. }
  113. return true;
  114. }
  115.  
  116. std::vector<std::string> transmutate(std::vector<std::string>
  117. v1,std::vector<std::string> v2)
  118. {
  119. if(v2[0]=="") return v1;
  120. //v2.reserve(v1.size()*v2.size());
  121.  
  122. uint64_t s = v1.size();
  123. //for(auto elem:v2)std::cout<<"n"<<elem;
  124. //std::cout<<"s = "<<s<<"n";
  125. auto v2c = v2;
  126. v2.reserve(v1.size()*v2.size());
  127.  
  128. uint64_t i=1;
  129. uint64_t a = floor(log2(v2.size()));
  130. //std::cout<<a;
  131. while(i<s)
  132. {
  133. copy(v2c.begin(),v2c.end(),std::back_inserter(v2));
  134. i++;
  135. }
  136. i=0;
  137. for(auto &elem : v1)
  138. {
  139. //std::cout<<i<<" ";
  140. for(uint64_t k=0;k<s;k++)
  141. {
  142. //std::cout<<elem<<" "<<v2[i]<<"n"<<i<<"n";
  143. v2[i]+=elem;
  144. //v2c[i]+=elem;
  145. //std::cout<<elem<<" "<<v2[i]<<"n"<<i<<"nn";
  146. i++;
  147. }
  148. }
  149. return v2;
  150. }
  151.  
  152.  
  153. std::vector<std::string> mutate(std::string bin)
  154. {
  155. auto v =vec;
  156. v={"",""};
  157. auto k = toStr(1);
  158. for(uint64_t i=0; i<bin.size(); i++)
  159. {
  160.  
  161. if(bin[i]==k[0]) //std::cout<<"1";
  162. v=transmutate(vecGrp[bin.size()-i-1],v);
  163. // for(auto elem : v)
  164. // std::cout<<elem<<std::endl;
  165. }
  166. return v;
  167. }
  168.  
  169. std::vector<std::string> algo(uint64_t n)
  170. {
  171. assert(n>1 && n<25);
  172. if(n==1)
  173. {
  174. return vec;
  175. }
  176. else
  177. {
  178. const auto bin = create_binary(n);
  179. auto check = bin.size();
  180.  
  181. if(check>index)
  182. {
  183. try
  184. {
  185. updateIndex(check);
  186. }
  187. catch(const std::exception& e)
  188. {
  189. std::cerr<<"Caught Exception in "<<__FILE__<<" at line "
  190. << __LINE__ << " as " << e.what();
  191. }
  192. }
  193. //std::cout << index;
  194. if( (n & (n-1)) ==0)
  195. return vecGrp[index-1];
  196. auto v = mutate(bin);
  197. return v;
  198. }
  199.  
  200. return vec;
  201. }
  202.  
  203. int main()
  204. {
  205. try
  206. {
  207. int n;
  208. std::cin >> n;
  209.  
  210.  
  211. std::ofstream file;
  212. std::ofstream file1;
  213. std::ofstream file2;
  214.  
  215. file1.open("ans.txt");
  216. file2.open("time.txt",std::ios::app);
  217. file2 << "n";
  218. std::vector<int> rng(n);
  219. std::iota(rng.begin(),rng.end(),1);
  220.  
  221. for(auto &i : rng)
  222. {
  223.  
  224. auto start = std::chrono::system_clock::now();
  225. /*
  226. std::stringstream ss;
  227. ss << i;
  228. std::string st = ss.str();
  229. std::string filename = "out/out"+ st +".txt";
  230. file.open(filename);
  231. */
  232. std::vector<std::string> v = algo(i);
  233. sort(v.begin(),v.end());
  234. //file << i << std::endl;
  235. std::cout << i << std::endl;
  236. file1 << v.size() <<"n";
  237. //print_to_file(i,v);
  238. /* for(const auto &elem : v)
  239. {
  240. file << elem << "n";
  241. //std::cout << elem << "n";
  242. }
  243. //file << std::endl;
  244. */
  245. auto endt = std::chrono::system_clock::now();
  246. //std::chrono::duration<long long uint64_t, std::nano> diff = endt-start;
  247. std::chrono::duration<long double> diff = endt-start;
  248.  
  249. file2 << i << " " << diff.count() << "n";
  250. std::cout << i << " " << diff.count() << "n";
  251. file.close();
  252. }
  253. return 0;
  254. }
  255. catch(const std::exception &e)
  256. {
  257. std::cerr<<"Caught Exception in "<<__FILE__<<" at line "
  258. << __LINE__ << " as " << e.what();
  259. }
  260. }
Add Comment
Please, Sign In to add comment