Advertisement
Josif_tepe

Untitled

Mar 2nd, 2024
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main() {
  5.     int p, n;
  6.     cin >> p >> n;
  7.     vector<int> v;
  8.     for(int i = 0; i < 100; i++) {
  9.         int tmp = p;
  10.         int treta_cifra = tmp % 10;
  11.         tmp /= 10;
  12.         int vtora_cifra = tmp % 10;
  13.         tmp /= 10;
  14.         int prva_cifra = tmp % 10;
  15.         tmp /= 10;
  16.         int sleden_broj = prva_cifra * prva_cifra + vtora_cifra * vtora_cifra + treta_cifra * treta_cifra;
  17.        
  18.        
  19.         v.push_back(p);
  20.         p = sleden_broj;
  21.         if(i + 1 == n) {
  22.             cout << p << endl;
  23.             return 0;
  24.         }
  25.     }
  26.     vector<int> reps;
  27.     for(int i = 0; i < v.size(); i++) {
  28.         int se_povtoruva = -1;
  29.         for(int j = i + 1; j < v.size(); j++) {
  30.             if(v[i] == v[j]) {
  31.                 se_povtoruva = j - 1;
  32.                 break;
  33.             }
  34.         }
  35.         if(se_povtoruva != -1) {
  36.             for(int j = i; j <= se_povtoruva; j++) {
  37.                
  38.                 reps.push_back(v[j]);
  39.             }
  40.             n -= i;
  41.             break;
  42.         }
  43.     }
  44.     int sz = reps.size();
  45.     if(n % sz == 0) {
  46.         cout << reps[sz - 1] << endl;
  47.     }
  48.     else {
  49.         cout << reps[n % sz] << endl;
  50.     }
  51.     return 0;
  52. }
  53.  
  54. // 89 145 42 20 4 16 37 58
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement