Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int main() {
- int p, n;
- cin >> p >> n;
- int niza[105];
- int j = 0;
- for(int i = 1; i <= 100; i++) {
- int tmp = p;
- int sum = 0;
- while(tmp > 0) {
- int cifra = tmp % 10;
- sum += cifra * cifra;
- tmp /= 10;
- }
- niza[j] = p;
- j++;
- p = sum;
- if(i == n) {
- cout << p << endl;
- return 0;
- }
- }
- int reps[100], r = 0;
- for(int i = 0; i < j; i++) {
- int se_povtoruva = -1;
- for(int k = i + 1; k < j; k++) {
- if(niza[i] == niza[k]) {
- se_povtoruva = k;
- break;
- }
- }
- if(se_povtoruva != -1) {
- for(int k = i; k < se_povtoruva; k++) {
- reps[r] = niza[k];
- r++;
- }
- n -= i;
- break;
- }
- }
- int ostatok = n % r;
- if(ostatok == 0) {
- cout << reps[r - 1] << endl;
- }
- else {
- cout << reps[ostatok] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement