fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. const int oo = 1e3 + 3;
  7.  
  8. int t, n, k;
  9. int a[oo];
  10. map<int, int> mp, visited;
  11.  
  12. void solve() {
  13. cin >> n;
  14. for (int i = 1; i <= n; i++) {
  15. cin >> a[i];
  16. }
  17. }
  18.  
  19. int next(int n) {
  20. int res = 0;
  21. for (int x = n % 10; n != 0; n /= 10) {
  22. res += x * x;
  23. }
  24. return res;
  25. }
  26.  
  27. int check_cycle(int n) {
  28. if (n == 1) {
  29. return 1;
  30. }
  31.  
  32. if (visited[n]) {
  33. return -1;
  34. }
  35.  
  36. cout << n <<" ";
  37.  
  38. visited[n] = 1;
  39.  
  40. int next_n = next(n);
  41. mp[n] = check_cycle(next_n);
  42.  
  43. return mp[n];
  44. }
  45.  
  46. void prerequisite() {
  47. for (int i = 1; i <= 729; i++) {
  48. mp[i] = check_cycle(i);
  49. }
  50. }
  51.  
  52. int32_t main() {
  53.  
  54. cin >> t;
  55. check_cycle(t);
  56.  
  57. // prerequisite();
  58.  
  59. // while(t--) {
  60. // solve();
  61. // }
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0s 5320KB
stdin
4
stdout
4 16 72 8 64 32