Advertisement
STEFAN_STOYANOV

Sequence

Dec 7th, 2022 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int cycle[8];
  4. int sqr_dig(int k)
  5. {
  6. int num=0,dig;
  7. while(k)
  8. {
  9. dig=k%10;
  10. num+=dig*dig;
  11. k/=10;
  12. }
  13. return num;
  14. }
  15. int is_in_cycle(int k)
  16. {
  17. for(int pos=0;pos<8;++pos)
  18. if(cycle[pos]==k)return pos;
  19. return -1;
  20. }
  21. int main()
  22. {
  23. ios_base::sync_with_stdio(false);
  24. cin.tie(0);
  25. cout.tie(0);
  26. cycle[0]=58;
  27. cycle[1]=89;
  28. cycle[2]=145;
  29. cycle[3]=42;
  30. cycle[4]=20;
  31. cycle[5]=4;
  32. cycle[6]=16;
  33. cycle[7]=37;
  34. int Tests;
  35. int a,v,n;
  36. int i,anscnt,posa,posv,cntcyc;
  37. cin>>Tests;
  38. while(Tests)
  39. {
  40. cin>>a>>v>>n;
  41. anscnt=0;
  42. i=1;
  43. if(a!=1&&is_in_cycle(a)==-1)
  44. {
  45. if(a==v)++anscnt;
  46. for(i=2;i<=n;++i)
  47. {
  48. a=sqr_dig(a);
  49. if(a==1)break;
  50. posa=is_in_cycle(a);
  51. if(posa!=-1)break;
  52. if(a==v)++anscnt;
  53. }
  54. }
  55. if(a==1)
  56. {if(v==1)anscnt+=n-i+1;}///without the braces the code does not work. I don't know why
  57. else
  58. {
  59. posv=is_in_cycle(v);
  60. if(posv!=-1)
  61. {
  62. n-=i;
  63. n++;
  64. ///excluding the first i-1 numbers
  65. anscnt+=n/8;
  66. n%=8;
  67. if(n!=0&&a==v)++anscnt;
  68. for(i=2;i<=n;++i)
  69. {
  70. a=sqr_dig(a);
  71. if(a==v)++anscnt;
  72. }
  73. }
  74. }
  75. cout<<anscnt<<endl;
  76. --Tests;
  77. }
  78. return 0;
  79. }
  80. /*
  81. 89 145 42 20 4 16 37 58
  82. */
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement