Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int cycle[8];
- int sqr_dig(int k)
- {
- int num=0,dig;
- while(k)
- {
- dig=k%10;
- num+=dig*dig;
- k/=10;
- }
- return num;
- }
- int is_in_cycle(int k)
- {
- for(int pos=0;pos<8;++pos)
- if(cycle[pos]==k)return pos;
- return -1;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- cycle[0]=58;
- cycle[1]=89;
- cycle[2]=145;
- cycle[3]=42;
- cycle[4]=20;
- cycle[5]=4;
- cycle[6]=16;
- cycle[7]=37;
- int Tests;
- int a,v,n;
- int i,anscnt,posa,posv,cntcyc;
- cin>>Tests;
- while(Tests)
- {
- cin>>a>>v>>n;
- anscnt=0;
- i=1;
- if(a!=1&&is_in_cycle(a)==-1)
- {
- if(a==v)++anscnt;
- for(i=2;i<=n;++i)
- {
- a=sqr_dig(a);
- if(a==1)break;
- posa=is_in_cycle(a);
- if(posa!=-1)break;
- if(a==v)++anscnt;
- }
- }
- if(a==1)
- {if(v==1)anscnt+=n-i+1;}///without the braces the code does not work. I don't know why
- else
- {
- posv=is_in_cycle(v);
- if(posv!=-1)
- {
- n-=i;
- n++;
- ///excluding the first i-1 numbers
- anscnt+=n/8;
- n%=8;
- if(n!=0&&a==v)++anscnt;
- for(i=2;i<=n;++i)
- {
- a=sqr_dig(a);
- if(a==v)++anscnt;
- }
- }
- }
- cout<<anscnt<<endl;
- --Tests;
- }
- return 0;
- }
- /*
- 89 145 42 20 4 16 37 58
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement